出现键盘时上移视图 [英] Move view up when keyboard is presented

查看:75
本文介绍了出现键盘时上移视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

The Question:

我有一个 ViewController 的子类中带有 UIScrollView

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

scrollView中有3个 UITextField s。其中2个使用数字键盘,而1个使用 UIPickerView 键盘。

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

问题在于,当键盘在显示时,它会隐藏 UITextField s。所以我想做的是在显示键盘时将 UIViewController 的视图向上移动。

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:

我已经在SO上搜索了这个问题,找到了一些答案,根据它们,我编写了这段代码:

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?

谢谢!

推荐答案

我为此使用了一个CocoaPod,名为IQKeyboardManager。

I use a CocoaPod for this called IQKeyboardManager.

https://github.com/hackiftekhar/IQKeyboardManager

设置起来非常简单,并且可以在全球范围内使用。

It is dead simple to set up and works globally.

这篇关于出现键盘时上移视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆