显示键盘时移动视图



问题:

我有一个 ViewController,其中有一个带有 UIScrollView的子类。

在卷轴上有3 UITextField s。其中2个带有编号键盘和1个带有UIPickerView键盘的键盘。

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

我已经尝试过的内容:

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

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

我尝试过的问题:

有时它效果很好,但是有时视图"跳"向上/跳下来,我不明白为什么。

有人看到我的代码可能导致此错误的任何问题?

谢谢!

我为此使用Cocoapod。

https://github.com/hackiftekhar/iqkeyboardmanager

设置并在全球工作很简单。

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

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。您只需要将它们添加到您的应用程序中,它将处理所有内容。

相关内容

  • 没有找到相关文章

最新更新