2017-04-24 52 views
0

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

然而,当我的VC加载我的看法改变很奇怪的方式高度

在IB keyboardHeigthConstraint是底部布局指南顶部和我的视图的约束(因为我是GIF格式的显示)和等于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

+0

这里有什么问题? – KKRocks

+0

你能看到我的视图是如何在加载时“跳跃”的。我觉得很奇怪。 – dand1

+0

很高兴知道我们的答案是否对您有所帮助。 –

回答

0

从你的GIF图片,我可以看到你的按钮是跳跃的,并且可能是问题。 尝试使用textView添加按钮的垂直约束,并且您的按钮将始终处于该位置。 如果你想支持所有的设备,你必须根据设备的大小来计算垂直约束。

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

你也可以用动画

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

高度实际上所有的答案上面已经不适合我,但我的最终变异提供了可行的解决方案是所有theese的合作answres

我想,关键问题是我决定改变底部约束。当我转移到第一名时,“跳跃”消失了

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) 
    } 
+0

很酷。为了以防万一,你甚至可以接受你自己的答案来说清楚。 –