Move view up when keyboard is presented

北战南征 提交于 2019-12-12 12:30:30

问题


The Question:

I have a ViewController that has a subclass with a UIScrollView in it.

In the scrollView there are 3 UITextFields. 2 of them with a numberPad keyboard and 1 with a UIPickerView keyboard.

The problem is that when the keyboards are presented, it hides the UITextFields. So what I'm trying to do is to move the UIViewController's view up when the keyboards are being shown.

What I've already tried:

I've searched this question on SO and I've found some answers, based on them I've wrote this code:

override func viewDidLoad() {
    super.viewDidLoad()

    //Keyboard notifications
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

//MARK: - keyboards

@objc fileprivate func keyboardWillShow(notification: Notification) {
    self.changeViewOriginBasedOnKeyboardPosition(notification: notification)
}

@objc fileprivate func keyboardWillHide(notification: Notification) {
    self.changeViewOriginBasedOnKeyboardPosition(notification: notification)
}

fileprivate func changeViewOriginBasedOnKeyboardPosition(notification: Notification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        let keyboardAnimationDuration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
        UIView.animate(withDuration: keyboardAnimationDuration != 0 ? keyboardAnimationDuration : 0.2, animations: {
            if self.controllerContentView.frame.origin.y == 0  {
                self.controllerContentView.frame.origin.y = -keyboardSize.height
            } else {
                self.controllerContentView.frame.origin.y = 0
            }
        })
    }
}

fileprivate func dismissKeyboard() {
    self.view.endEditing(true)
}

//Touch handling

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.dismissKeyboard()
}

The problem with what I've tried:

Sometimes it works great, but sometimes the view "jumps" back up/"jumps" down and I can't understand why.

Does anybody see any problem with my code that can cause this bug?

Thank you!


回答1:


I use a CocoaPod for this called IQKeyboardManager.

https://github.com/hackiftekhar/IQKeyboardManager

It is dead simple to set up and works globally.




回答2:


You need to set height as per your keyboard hide. There is four observer for Keyboard available.

1) UIKeyboardWillShow
2) UIKeyboardDidShow
3) UIKeyboardWillHide
4) UIKeyboardDidHide

You need to register keyboard observer:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardShowNotification), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardHideNotification), name:NSNotification.Name.UIKeyboardWillHide, object: nil)

Here is method observer method:

func keyboardShowNotification(notification:NSNotification){

    var userInfo = notification.userInfo!
    var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    keyboardFrame = self.view.convert(keyboardFrame, from: nil)

    var contentInset:UIEdgeInsets = YOUR_SCROLLVIEW_OBJ.contentInset
    contentInset.bottom = keyboardFrame.size.height
    YOUR_SCROLLVIEW_OBJ.contentInset = contentInset
}

func keyboardHideNotification(notification:NSNotification){
    let contentInset:UIEdgeInsets = UIEdgeInsets.zero
    YOUR_SCROLLVIEW_OBJ = contentInset
}

Another best key to handle keyboard is IQKeyboardManager. You just have to add them to your application and it will handle everything.



来源:https://stackoverflow.com/questions/47698632/move-view-up-when-keyboard-is-presented

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!