将UIView设置为键盘高度

时间:2017-04-24 13:46:43

标签: ios uiviewcontroller

我想将视图底部约束设置为等于键盘高度。 在VC的生命周期中,键盘始终处于活动状态。

然而,当我的VC加载我的视图时,以一种奇怪的方式改变它的高度(正如我在gif中所示)

在IB键盘中,HeigthConstraint约束底部布局指南Top和我的View并等于0

 @IBOutlet weak var keyboardHeigthConstraint: NSLayoutConstraint!

   override func viewDidLoad() {
        super.viewDidLoad()
        cardNumberTextField.becomeFirstResponder()

        NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillChangeFrame , object: nil)
    }

func handleKeyboardNotification(notification: NSNotification) {

    if let userInfo = notification.userInfo {
        let keyboardFrame: CGRect = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

        DispatchQueue.main.async {
            self.keyboardHeigthConstraint.constant = keyboardFrame.size.height
            self.view.layoutIfNeeded()
        }
    }

什么可能导致这种奇怪的行为?

enter image description here

4 个答案:

答案 0 :(得分:1)

您的代码中存在一些问题:

  • cardNumberTextField.becomeFirstResponder()应该在viewWillAppear中调用;
  • 在您订阅键盘通知后致电cardNumberTextField.becomeFirstResponder();
  • 取消订阅viewDidDisappear;
  • 中的键盘通知
  • 更新约束而不将其包含在DispatchQueue.main.async

我认为它应该有所帮助。

@IBOutlet weak var keyboardHeigthConstraint: NSLayoutConstraint!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
    cardNumberTextField.becomeFirstResponder()
}

override func viewDidDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self)
    super.viewDidDisappear(animated)
}

func handleKeyboardNotification(notification: NSNotification) {
    guard let userInfo = notification.userInfo,
        let frameValue = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else {

        return
    }

    let keyboardFrame = frameValue.cgRectValue
    keyboardHeigthConstraint.constant = keyboardFrame.height
    view.layoutIfNeeded()
}

答案 1 :(得分:1)

您还可以使用

设置高度动画
let keyboardDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue

     UIView.animate(withDuration: keyboardDuration, animations: { 
        DispatchQueue.main.async {
           self.keyboardHeigthConstraint.constant = keyboardFrame.size.height
           self.view.layoutIfNeeded()
       }               
   })

答案 2 :(得分:0)

从你的gif图像我可以看到你的按钮正在跳跃,这可能是问题所在。 尝试使用textView添加按钮的垂直约束,您的按钮将始终位于该位置。 如果要支持所有设备,则必须根据设备大小计算该垂直约束。

答案 3 :(得分:0)

实际上上面的所有答案都没有为我提供工作解决方案,但我的最终变体是所有学生的合作答案

我想,关键问题是我决定改变底部约束。当我转移到顶部时,"跳跃"消失

 override func viewWillAppear(_ animated: Bool) {
        cardNumberTextField.becomeFirstResponder()
         NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillChangeFrame , object: nil)
    }

    override func viewDidDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(self)
        super.viewDidDisappear(animated)
    }


    func handleKeyboardNotification(notification: NSNotification) {

        guard let userInfo = notification.userInfo,
            let frameValue = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue
            else { return }

        let duration:TimeInterval = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSNumber)?.doubleValue ?? 0

        print(keyboardHeigthConstraint.constant)
        UIView.animate(withDuration: duration, animations: {
//hardcoded 116 - it is buttonHeight + navBarHeight
            self.keyboardHeigthConstraint.constant = UIScreen.main.bounds.height - frameValue.cgRectValue.size.height - 116
            self.view.layoutIfNeeded()
        })
         print(keyboardHeigthConstraint.constant)
    }