隐藏键盘Swift 4后,视图底部的元素消失 [英] Element at the bottom of view disappear after hiding the keyboard Swift 4
问题描述
我在屏幕底部有2个按钮.为了显示和隐藏键盘,我实现了以下代码,但没有在底部覆盖我的2个按钮.
I have 2 button at the bottom of my screen.I implement the code below in order to show and hide keyboard but do not cover my 2 button at the bottom.
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(MyViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(MyViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
//here will hide the keyboard when tap the text view 2 times
let tap = UITapGestureRecognizer(target: self, action: #selector(hideKeyBoard))
self.statusTextView.addGestureRecognizer(tap)
}
@objc func hideKeyBoard(sender: UITapGestureRecognizer? = nil){
statusTextView.endEditing(true)
}
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if let window = self.view.window?.frame {
// We're not just minusing the kb height from the view height because
// the view could already have been resized for the keyboard before
self.view.frame = CGRect(x: self.view.frame.origin.x,
y: self.view.frame.origin.y,
width: self.view.frame.width,
height: window.origin.y + window.height - keyboardSize.height)
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let viewHeight = self.view.frame.height
self.view.frame = CGRect(x: self.view.frame.origin.x,
y: self.view.frame.origin.y,
width: self.view.frame.width,
height: viewHeight + keyboardSize.height)
}
}
使用上面的代码,我成功地显示和隐藏了键盘,但底部没有覆盖任何元素.
With the code above,I successfully show and hide the keyboard without cover any element at the bottom.
但是发生了奇怪的事情,当我点击textview
2次时,键盘隐藏了,但屏幕底部的2个按钮却消失了.
But strange thing happen,when I tapped the textview
2 times,the keyboard is hide but the 2 button at bottom of screen is disappear.
所以我的问题是,为什么隐藏键盘时元素会消失?以及如何解决呢?
So my question is,why the element will disappear when hide the keyboard? And how to solve it??
推荐答案
使用UIKeyboardFrameEndUserInfoKey
代替UIKeyboardFrameBeginUserInfoKey
,因为UIKeyboardFrameBeginUserInfoKey
可以在keyboardWillShow
和keyboardWillHide
Use UIKeyboardFrameEndUserInfoKey
instead of UIKeyboardFrameBeginUserInfoKey
as UIKeyboardFrameBeginUserInfoKey
can return different values in keyboardWillShow
and keyboardWillHide
UIKeyboardFrameBeginUserInfoKey
–当前键盘状态更改开始处的键盘框架.
UIKeyboardFrameEndUserInfoKey
–当前键盘状态更改结束时键盘的框架.
UIKeyboardFrameBeginUserInfoKey
– frame of the keyboard at the beginning of the current keyboard state change.
UIKeyboardFrameEndUserInfoKey
– frame of the keyboard at the end of the current keyboard state change.
这篇关于隐藏键盘Swift 4后,视图底部的元素消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!