出现键盘时向上移动视图
Move view up when keyboard is presented
问题:
我有一个 ViewController
,它有一个包含 UIScrollView
的子类。
scrollView 中有 3 个 UITextField
。其中 2 个带有 numberPad 键盘,1 个带有 UIPickerView
键盘。
问题是当键盘出现时,它隐藏了 UITextField
s。所以我要做的是在显示键盘时将 UIViewController
的视图向上移动。
我已经尝试过的:
我在 SO 上搜索了这个问题,找到了一些答案,基于它们我写了这段代码:
override func viewDidLoad() {
super.viewDidLoad()
//Keyboard notifications
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
//MARK: - keyboards
@objc fileprivate func keyboardWillShow(notification: Notification) {
self.changeViewOriginBasedOnKeyboardPosition(notification: notification)
}
@objc fileprivate func keyboardWillHide(notification: Notification) {
self.changeViewOriginBasedOnKeyboardPosition(notification: notification)
}
fileprivate func changeViewOriginBasedOnKeyboardPosition(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardAnimationDuration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
UIView.animate(withDuration: keyboardAnimationDuration != 0 ? keyboardAnimationDuration : 0.2, animations: {
if self.controllerContentView.frame.origin.y == 0 {
self.controllerContentView.frame.origin.y = -keyboardSize.height
} else {
self.controllerContentView.frame.origin.y = 0
}
})
}
}
fileprivate func dismissKeyboard() {
self.view.endEditing(true)
}
//Touch handling
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.dismissKeyboard()
}
我试过的问题:
有时效果很好,但有时视图 "jumps" 备份/"jumps" 下降,我不明白为什么。
有没有人发现我的代码存在任何可能导致此错误的问题?
谢谢!
您需要根据键盘隐藏设置高度。有四个键盘观察器可用。
1) UIKeyboardWillShow
2) UIKeyboardDidShow
3) UIKeyboardWillHide
4) UIKeyboardDidHide
您需要注册键盘观察者:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardShowNotification), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardHideNotification), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
这里是方法观察者方法:
func keyboardShowNotification(notification:NSNotification){
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.view.convert(keyboardFrame, from: nil)
var contentInset:UIEdgeInsets = YOUR_SCROLLVIEW_OBJ.contentInset
contentInset.bottom = keyboardFrame.size.height
YOUR_SCROLLVIEW_OBJ.contentInset = contentInset
}
func keyboardHideNotification(notification:NSNotification){
let contentInset:UIEdgeInsets = UIEdgeInsets.zero
YOUR_SCROLLVIEW_OBJ = contentInset
}
另一个处理键盘的最佳键是IQKeyboardManager。您只需将它们添加到您的应用程序中,它就会处理所有事情。
我为此使用了一个名为 IQKeyboardManager 的 CocoaPod。
https://github.com/hackiftekhar/IQKeyboardManager
设置起来非常简单,并且可以在全球范围内使用。
问题:
我有一个 ViewController
,它有一个包含 UIScrollView
的子类。
scrollView 中有 3 个 UITextField
。其中 2 个带有 numberPad 键盘,1 个带有 UIPickerView
键盘。
问题是当键盘出现时,它隐藏了 UITextField
s。所以我要做的是在显示键盘时将 UIViewController
的视图向上移动。
我已经尝试过的:
我在 SO 上搜索了这个问题,找到了一些答案,基于它们我写了这段代码:
override func viewDidLoad() {
super.viewDidLoad()
//Keyboard notifications
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
//MARK: - keyboards
@objc fileprivate func keyboardWillShow(notification: Notification) {
self.changeViewOriginBasedOnKeyboardPosition(notification: notification)
}
@objc fileprivate func keyboardWillHide(notification: Notification) {
self.changeViewOriginBasedOnKeyboardPosition(notification: notification)
}
fileprivate func changeViewOriginBasedOnKeyboardPosition(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardAnimationDuration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
UIView.animate(withDuration: keyboardAnimationDuration != 0 ? keyboardAnimationDuration : 0.2, animations: {
if self.controllerContentView.frame.origin.y == 0 {
self.controllerContentView.frame.origin.y = -keyboardSize.height
} else {
self.controllerContentView.frame.origin.y = 0
}
})
}
}
fileprivate func dismissKeyboard() {
self.view.endEditing(true)
}
//Touch handling
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.dismissKeyboard()
}
我试过的问题:
有时效果很好,但有时视图 "jumps" 备份/"jumps" 下降,我不明白为什么。
有没有人发现我的代码存在任何可能导致此错误的问题?
谢谢!
您需要根据键盘隐藏设置高度。有四个键盘观察器可用。
1) UIKeyboardWillShow
2) UIKeyboardDidShow
3) UIKeyboardWillHide
4) UIKeyboardDidHide
您需要注册键盘观察者:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardShowNotification), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardHideNotification), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
这里是方法观察者方法:
func keyboardShowNotification(notification:NSNotification){
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.view.convert(keyboardFrame, from: nil)
var contentInset:UIEdgeInsets = YOUR_SCROLLVIEW_OBJ.contentInset
contentInset.bottom = keyboardFrame.size.height
YOUR_SCROLLVIEW_OBJ.contentInset = contentInset
}
func keyboardHideNotification(notification:NSNotification){
let contentInset:UIEdgeInsets = UIEdgeInsets.zero
YOUR_SCROLLVIEW_OBJ = contentInset
}
另一个处理键盘的最佳键是IQKeyboardManager。您只需将它们添加到您的应用程序中,它就会处理所有事情。
我为此使用了一个名为 IQKeyboardManager 的 CocoaPod。
https://github.com/hackiftekhar/IQKeyboardManager
设置起来非常简单,并且可以在全球范围内使用。