我在 View 的下半部分有一个文本字段,因此我使用下面的函数来移动我的 View 。 但实际上,除了键盘顶部的黑色区域之外,一切看起来都像我想要的那样。如何处理它?任何帮助表示赞赏。 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/