【问题标题】:Move view up when keyboard is presented出现键盘时向上移动视图
【发布时间】:2018-05-21 18:30:31
【问题描述】:

问题:

我有一个 ViewController,它有一个包含 UIScrollView 的子类。

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

问题是当键盘出现时,它隐藏了UITextFields。所以我要做的是在显示键盘时向上移动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()
}

我尝试过的问题:

有时效果很好,但有时视图“跳”回/“跳”下,我不明白为什么。

有人发现我的代码有什么问题会导致这个错误吗?

谢谢!

【问题讨论】:

  • 您在controllerContentView 上设置了frame,但如果有自动布局更新,这些约束将接管。相反,更改controllerContentView 的约束,然后在UIView.animate 块内调用view.setNeedsLayout()

标签: ios iphone swift cocoa-touch uitextfield


【解决方案1】:

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

https://github.com/hackiftekhar/IQKeyboardManager

在全球范围内设置和工作非常简单。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 您可以使用 IQKeyboardManager。它会照顾键盘的每一件事。
    猜你喜欢
    • 1970-01-01
    • 2013-09-06
    • 2015-11-29
    • 2014-02-07
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多