键盘高度变化观察器
Keyboard height change observer
如何检测键盘高度变化,或使用 Swift 在 iOS 中检测键盘变化。
我可以为我的应用程序添加一个观察者来检测键盘是否显示:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentView.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentView.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
我正在根据那个改变我的按钮位置:
func keyboardWillShow(notification: NSNotification) {
animateTextFieldWithKeyboard(notification)
}
func keyboardWillHide(notification: NSNotification) {
animateTextFieldWithKeyboard(notification)
}
func animateTextFieldWithKeyboard(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! UInt
// baseContraint is your Auto Layout constraint that pins the
// text view to the bottom of the superview.
if notification.name == UIKeyboardWillShowNotification {
if (BottomConstraint.constant == 0) {
BottomConstraint.constant += keyboardSize.height
}
// move up
}
else {
BottomConstraint.constant = 0
// move down
}
view.setNeedsUpdateConstraints()
let options = UIViewAnimationOptions(rawValue: curve << 16)
UIView.animateWithDuration(duration, delay: 0, options: options,
animations: {
self.view.layoutIfNeeded()
},
completion: nil
)
}
如您在屏幕截图中所见,一切正常:
但是当我将键盘类型更改为表情符号时,问题就来了。它隐藏了我的 textField 和我的 Button,所以我想根据键盘新高度
更改按钮和 TextFiend 的位置
如果您在 Xcode 文档中搜索 UIKeyboardWillShowNotification
,您会找到有关 UIWindow 的部分,该部分末尾有 table 通知。
我建议尝试 UIKeyboardWillChangeFrameNotification
。
找到答案的时间:大约 30 秒。
我用这个,所有的通知都被触发了。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
let keyboardSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
if keyboard == false{
keyboard = true
lastKeyboardHeight = keyboardSize.height
chatDetailView.frame.origin.y = chatDetailView.frame.origin.y-(keyboardSize.height-bottomMenu.frame.height)
}
}
@objc func keyboardWillChange(notification: NSNotification) {
let keyboardSize1 = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
if keyboard == true && lastKeyboardHeight != keyboardSize1.height {
if lastKeyboardHeight < keyboardSize1.height{
let keyboardDifference: CGFloat = keyboardSize1.height-lastKeyboardHeight
chatDetailView.frame.origin.y -= keyboardDifference
} else {
let keyboardDifference: CGFloat = lastKeyboardHeight-keyboardSize1.height
chatDetailView.frame.origin.y += keyboardDifference
}
lastKeyboardHeight = keyboardSize1.height
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if keyboard == true {
keyboard = false
chatDetailView.frame.origin.y = chatDetailView.frame.origin.y+(lastKeyboardHeight-bottomMenu.frame.height)
}
}
如何检测键盘高度变化,或使用 Swift 在 iOS 中检测键盘变化。
我可以为我的应用程序添加一个观察者来检测键盘是否显示:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentView.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentView.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
我正在根据那个改变我的按钮位置:
func keyboardWillShow(notification: NSNotification) {
animateTextFieldWithKeyboard(notification)
}
func keyboardWillHide(notification: NSNotification) {
animateTextFieldWithKeyboard(notification)
}
func animateTextFieldWithKeyboard(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! UInt
// baseContraint is your Auto Layout constraint that pins the
// text view to the bottom of the superview.
if notification.name == UIKeyboardWillShowNotification {
if (BottomConstraint.constant == 0) {
BottomConstraint.constant += keyboardSize.height
}
// move up
}
else {
BottomConstraint.constant = 0
// move down
}
view.setNeedsUpdateConstraints()
let options = UIViewAnimationOptions(rawValue: curve << 16)
UIView.animateWithDuration(duration, delay: 0, options: options,
animations: {
self.view.layoutIfNeeded()
},
completion: nil
)
}
如您在屏幕截图中所见,一切正常:
但是当我将键盘类型更改为表情符号时,问题就来了。它隐藏了我的 textField 和我的 Button,所以我想根据键盘新高度
更改按钮和 TextFiend 的位置如果您在 Xcode 文档中搜索 UIKeyboardWillShowNotification
,您会找到有关 UIWindow 的部分,该部分末尾有 table 通知。
我建议尝试 UIKeyboardWillChangeFrameNotification
。
找到答案的时间:大约 30 秒。
我用这个,所有的通知都被触发了。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
let keyboardSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
if keyboard == false{
keyboard = true
lastKeyboardHeight = keyboardSize.height
chatDetailView.frame.origin.y = chatDetailView.frame.origin.y-(keyboardSize.height-bottomMenu.frame.height)
}
}
@objc func keyboardWillChange(notification: NSNotification) {
let keyboardSize1 = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
if keyboard == true && lastKeyboardHeight != keyboardSize1.height {
if lastKeyboardHeight < keyboardSize1.height{
let keyboardDifference: CGFloat = keyboardSize1.height-lastKeyboardHeight
chatDetailView.frame.origin.y -= keyboardDifference
} else {
let keyboardDifference: CGFloat = lastKeyboardHeight-keyboardSize1.height
chatDetailView.frame.origin.y += keyboardDifference
}
lastKeyboardHeight = keyboardSize1.height
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if keyboard == true {
keyboard = false
chatDetailView.frame.origin.y = chatDetailView.frame.origin.y+(lastKeyboardHeight-bottomMenu.frame.height)
}
}