【问题标题】:UiTextfield does not move up when Keyboard pops up ios 11 (Working on Previous versions)ios 11 弹出键盘时 UiTextfield 不上移 (Working on Previous versions)
【发布时间】:2017-09-30 16:08:18
【问题描述】:

问题:在 ios 11 中不工作(但在 ios 8 中工作)

以下代码是用 Swift 2.0 编写的。但是我的应用程序太大,无法一次性迁移代码并发布更新。

目标: 我想发布一个带有 xcode 7 的版本,但是在 ios 11 上调试时我得到了一个“开发者磁盘映像”。 那么如何在不迁移代码的情况下修复错误

func viewDidLoad(){

    super.baseScrolllView = scrollView
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardAdjust:"), name:UIKeyboardWillShowNotification, object: nil);
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardAdjust:"), name:UIKeyboardWillHideNotification, object: nil);
}

基类中的代码

var keyboardIsVisible = false
var baseScrolllView: UIScrollView!
func keyboardAdjust(notification: NSNotification) {

    let info = notification.userInfo!
    let keyboardHeight:CGFloat = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().size.height
    let duration:Double = info[UIKeyboardAnimationDurationUserInfoKey] as! Double

    var userInfo = notification.userInfo!
    var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
    keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil)

    if notification.name == UIKeyboardWillShowNotification && keyboardIsVisible == false{

        keyboardIsVisible = true

        UIView.animateWithDuration(duration, animations: { ()

            var contentInset:UIEdgeInsets = self.baseScrolllView.contentInset
            contentInset.bottom = keyboardFrame.size.height
            self.baseScrolllView.contentInset = contentInset



        })


    }else {
        keyboardIsVisible = false

        UIView.animateWithDuration(duration, animations: { ()
            var contentInset:UIEdgeInsets = UIEdgeInsetsZero
            self.baseScrolllView.contentInset = contentInset
        })
    }

}

【问题讨论】:

  • 看到这个link
  • @AdityaSrivastava 感谢您的回复............你的意思是我没有正确计算键盘的高度
  • 是的,我认为你的计算不正确。

标签: ios swift cocoa-touch


【解决方案1】:

这就是我获得键盘高度的方法

@objc func keyboardWillShow(_ notification: Notification) {
    guard let userInfo = notification.userInfo, 
          let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue, 
          let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double else { return }

    containerViewBottomConstraint.constant = keyboardSize.height
    UIView.animate(withDuration: duration) { 
        self.containerView.layoutIfNeeded()
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    相关资源
    最近更新 更多