[英]How to prevent keyboard from moving the view up when it is already shown
I got the keyboard to move up when I click a textfield but as soon as I click another textfield It move the view up even more.当我单击一个文本字段时,我让键盘向上移动,但是一旦我单击另一个文本字段,它就会将视图向上移动得更多。 Thank you for your help.
感谢您的帮助。
func keyboardStates() {
NotificationCenter.default.addObserver(self, selector:
#selector(keyboardWillChange(notification:)), name:
UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector:
#selector(keyboardWillChange(notification:)), name:
UIResponder.keyboardWillHideNotification, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillChange(notification: Notification){
guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
return
}
if notification.name == UIResponder.keyboardWillShowNotification {
view.frame.origin.y = -keyboardRect.height/3
} else {
view.frame.origin.y = 0
}
}
Try the below method,试试下面的方法,
@objc
func keyboardWillChange(_ notification: NSNotification) {
guard let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return }
guard let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt else { return }
guard let startingFrame = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else { return }
guard let endingFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
let deltaY = endingFrame.origin.y - startingFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
}, completion: nil)
}
var keyboardHasShown = false
@objc func keyboardWillChange(notification: Notification){
guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
return
}
if notification.name == UIResponder.keyboardWillShowNotification {
if keyboardHasShown == false {
view.frame.origin.y = -keyboardRect.height/3
}
keyboardHasShown = true
} else {
view.frame.origin.y = 0
keyboardHasShown = false
}
}
Well doing it per view controller basis will be difficult and time consuming, i would recommend to use this fantastic library for such tasks, it supports, scroll view, table view and collection view.在每个视图控制器基础上做得好将是困难和耗时的,我建议将这个出色的库用于此类任务,它支持滚动视图、表格视图和集合视图。
https://github.com/hackiftekhar/IQKeyboardManager https://github.com/hackiftekhar/IQKeyboardManager
Give it a try and i assure you will like it too.试一试,我保证你也会喜欢它。
Regards, Harish问候, 哈里斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.