我在视图的下半部分有一个文本字段,因此我正在使用下面的函数移动我的视图。
但是实际上,除了键盘顶部的黑色区域外,一切都看起来像我想要的那样。如何处理它?
here's the screen
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(ViewControllerForTextfield.keyboardWillShow(_:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil);
NotificationCenter.default.addObserver(self, selector: #selector(ViewControllerForTextfield.keyboardWillHide(_:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil);
textField.delegate = self
super.viewDidLoad()
}
func keyboardWillShow(_ sender: Notification) {
if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height + 100
}
}
}
func keyboardWillHide(_ sender: Notification) {
if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
}
}
}
最佳答案
我认为310涨幅太大。对于自定义键盘,确定键盘的高度并不容易。但是,this post解释了如何获取键盘的高度。
编辑:
也许您还想替换从文本字段到屏幕底部的距离差?
关于ios - 移动键盘时出现黑色区域,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40562382/