【发布时间】:2018-09-10 21:52:01
【问题描述】:
我使用下面的代码在键盘显示和隐藏时上下移动屏幕
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector:#selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
}
}
它适用于除 iPhone x 以外的所有设备(我认为问题是 iPhone X 底部浪费了空间)
问题是键盘大小在显示之前和之后发生变化,导致视图上下移动......
谁能帮忙?
【问题讨论】:
-
好吧,我遇到的问题与我最终使用
iqKeyboardmanager并设置输入字段和键盘 40 之间的距离相同。 -
再次让你惊叹@PratikPrajapati :) 我可以看看你的代码吗?
标签: ios swift uikeyboard