简体   繁体   English

当键盘弹出ios 11时,UiTextfield不会向上移动(使用早期版本)

[英]UiTextfield does not move up when Keyboard pops up ios 11 (Working on Previous versions)

PROBLEM : not working in ios 11 (However working in ios 8) 问题:在ios 11中不起作用(但是在ios 8中工作)

The following code is written in Swift 2.0. 以下代码是用Swift 2.0编写的。 But my application is too large to migrate the code at one go and to release an update. 但是我的应用程序太大,无法一次迁移代码并发布更新。

Aim : I want to give a release with xcode 7 but i get a 'developer disk image' when debugging on ios 11. So how can i fix the bug without migrating the code 目的:我想使用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);
}

Code in Base class 基类中的代码

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
        })
    }

}

This is how I get the height of keyboard 这就是我得到键盘高度的方法

@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()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM