【问题标题】:How do I display the keyboard and move up the UIView at the same time when the view appears?如何在视图出现的同时显示键盘并向上移动 UIView?
【发布时间】:2020-10-28 10:22:31
【问题描述】:

我创建了一个视图控制器,其 UIView 包含一个文本字段(注意:视图控制器显示为模式)。当您点击文本字段时,会出现一个键盘,并且 UIView 会在屏幕上向上移动,这样文本字段就不会被遮挡。但是,我的目标是从一开始就显示键盘和(不被遮挡的)UIView,当模式最初出现时,我正在努力实现。

我尝试将 textField.becomeFirstResponder() 插入 viewDidLoad,但这会显示键盘而不会将 UIView 移动到所需的(即可见的)位置。我也尝试将其插入 viewDidAppear,但这首先显示 UIView,然后停顿一秒钟,然后显示键盘并在非常尴尬的过渡中向上移动 UIView。

如果有人能提供一些建议来解决这个问题,那就太好了。我的代码如下。

@IBOutlet var textField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .darkGray
    
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    //The line below will display the keyboard over the UIView, thus obscuring it.
    textField.becomeFirstResponder()
}

@objc func keyboardWillShow(_ notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardRectangle = keyboardFrame.cgRectValue
        let keyboardHeight = keyboardRectangle.height
    }
    let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
    self.view.frame.origin.y = 0 - keyboardSize!.height
}

以下是视觉参考。

【问题讨论】:

标签: ios swift uiview uitextfield uiresponder


【解决方案1】:

您可以添加动画块以使您的视图看起来更好,您还需要匹配键盘动画的速度,如下所示:

        if let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double {
            UIView.animate(withDuration: duration, animations: { [weak self] in
                self?.view.layoutIfNeeded()
            })
        }

这应该添加到你的keyboardWillShow函数的末尾。

如果您将textField.becomeFirstResponder() 移至viewWillAppear 可能会更好。

【讨论】:

  • 感谢您的回复,霍萨姆。我已尝试实施您的建议,但当模态出现时,键盘仍会弹出以覆盖 UIVIew。
【解决方案2】:

引用 (IBOutlet) 视图的底部约束,将其命名为 bottomConstraint 或您喜欢的任何名称。

然后,正如您在keyboardWillShow 选择器中所做的那样,提取键盘高度,并将该高度分配给bottomConstraintconstant 属性。如果需要,可以添加动画。

import UIKit

class SecondViewController: UIViewController {

    @IBOutlet var textField: UITextField!
    @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .darkGray
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
        
        //The line below will display the keyboard over the UIView, thus obscuring it.
        textField.becomeFirstResponder()
    }

    @objc func keyboardWillShow(_ notification: Notification) {
        let info = notification.userInfo!
        let kbHeight = (info[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
        
        bottomConstraint.constant = -kbHeight
        
        let duration: TimeInterval = (info[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
        
        UIView.animate(withDuration: duration) { self.view.layoutIfNeeded() }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 2015-11-29
    • 2018-12-16
    相关资源
    最近更新 更多