Ios 显示键盘时向上移动视图
问题: 我有一个Ios 显示键盘时向上移动视图,ios,iphone,swift,cocoa-touch,uitextfield,Ios,Iphone,Swift,Cocoa Touch,Uitextfield,问题: 我有一个ViewController,其中有一个子类带有UIScrollView 在滚动视图中有3个UITextFields。其中2个带有数字键盘,1个带有UIPickerView键盘 问题在于,当显示键盘时,它会隐藏UITextFields。因此,我试图做的是在显示键盘时向上移动UIViewController的视图 我已经尝试过的: 我搜索了这个问题,找到了一些答案,根据这些答案,我编写了以下代码: override func viewDidLoad() { super.vi
ViewController
,其中有一个子类带有UIScrollView
在滚动视图中有3个UITextField
s。其中2个带有数字键盘,1个带有UIPickerView
键盘
问题在于,当显示键盘时,它会隐藏UITextField
s。因此,我试图做的是在显示键盘时向上移动UIViewController
的视图
我已经尝试过的:
我搜索了这个问题,找到了一些答案,根据这些答案,我编写了以下代码:
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()
}
override func viewDidLoad(){
super.viewDidLoad()
//键盘通知
NotificationCenter.default.addObserver(self,选择器:#选择器(keyboardWillShow),名称:NSNotification.name.UIKeyboardWillShow,对象:nil)
NotificationCenter.default.addObserver(self,选择器:#选择器(keyboardWillHide),名称:NSNotification.name.UIKeyboardWillHide,对象:nil)
}
//马克:-键盘
@objc fileprivate func键盘将显示(通知:通知){
self.changeViewOriginBasedOnKeyboardPosition(通知:通知)
}
@objc fileprivate func键盘将隐藏(通知:通知){
self.changeViewOriginBasedOnKeyboardPosition(通知:通知)
}
fileprivate func changeViewOriginBasedOnKeyboardPosition(通知:通知){
如果让keyboardSize=(notification.userInfo?[UIKeyboardFrameEndUserInfoKey]作为?NSValue)?.cgRectValue{
让keyboardAnimationDuration=notification.userInfo![UIKeyboardAnimationDurationUserInfoKey]作为!时间间隔
UIView.animate(持续时间:keyboardAnimationDuration!=0?keyboardAnimationDuration:0.2,动画:{
如果self.controllerContentView.frame.origin.y==0{
self.controllerContentView.frame.origin.y=-keyboardSize.height
}否则{
self.controllerContentView.frame.origin.y=0
}
})
}
}
fileprivate func dismissKeyboard(){
self.view.endEditing(true)
}
//触摸处理
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
self.dismissKeyboard()
}
我尝试过的问题:
有时效果很好,但有时视图“跳”起来/跳下来,我不明白为什么
有人看到我的代码有任何问题会导致这个错误吗
谢谢大家! 您需要根据键盘隐藏设置高度。键盘有四个观察者可用
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
}
另一个处理键盘的最佳键是。您只需将它们添加到您的应用程序中,它就会处理所有事情。我使用CocoaPod来处理这个称为IQKeyboardManager的应用程序
设置起来非常简单,并且可以在全局范围内工作。请查看此答案您正在
controllerContentView
上设置框架
,但是如果有自动布局更新,这些约束将接管。相反,更改controllerContentView
的约束,然后在UIView.animate
块内调用view.setNeedsLayout()
。可以使用IQKeyboardManager。它会照顾到关于keybaord的一切。