我想设置我的视图底部约束等于键盘高度。 键盘在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()
}
}
什么会导致这种奇怪的行为?
这里有什么问题? – KKRocks
你能看到我的视图是如何在加载时“跳跃”的。我觉得很奇怪。 – dand1
很高兴知道我们的答案是否对您有所帮助。 –