键盘出现时向上移动视图

时间:2017-12-07 15:39:38

标签: ios iphone swift cocoa-touch uitextfield

问题:

我有一个ViewController,其子类中包含UIScrollView

在scrollView中有3个UITextField。其中2个带有数字键盘键盘,1个带有UIPickerView键盘。

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

我已尝试过的内容:

我在SO上搜索了这个问题,我找到了一些答案,基于它们,我写了这段代码:

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

我尝试过的问题:

有时它效果很好,但有时视图会“跳”回“跳”,“我跳不清楚”。我无法理解为什么。

是否有人发现我的代码有任何可能导致此错误的问题?

谢谢!

2 个答案:

答案 0 :(得分:0)

您需要根据键盘隐藏设置高度。键盘有四个观察员可用。

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

您需要注册键盘观察者:

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)

这是方法观察者方法:

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
}

处理键盘的另一个最佳键是IQKeyboardManager。您只需将它们添加到您的应用程序中即可处理所有内容。

答案 1 :(得分:0)

我为这个名为IQKeyboardManager使用CocoaPod。

https://github.com/hackiftekhar/IQKeyboardManager

设置和全球工作非常简单。