找出UIKeyboard.frame与其他框架何时相交? [英] Find out when UIKeyboard.frame intersects with other frame?
本文介绍了找出UIKeyboard.frame与其他框架何时相交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要找出文本字段何时成为第一个响应者,以通知我将要显示的键盘是否会阻塞UITextField.如果可以,我想调整scrollview属性.
I need to find out when the textfield becomes the first responder to notify me whether the keyboard that's going to show will obstruct the UITextField. If it does, I wanna adjust the scrollview properties.
到目前为止,我已经进行了此设置.我正在监听UIKeyboardWillShow通知,该通知调用以下选择器:
So far I have this setup. I'm listening for UIKeyboardWillShow notifications that calls the following selector:
func keyboardWillAppear(notification:NSNotification)
{
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
{
if keyboardSize.intersects(textField.frame)
{
print("It intersects")
}
else
{
print("Houston, we have a problem")
}
}
注意:我尝试使用UIKeyboardDidShow,但仍然没有成功. UITextField是scrollView的子视图.
Note: I tried with UIKeyboardDidShow but still no success. UITextField is a subview of the scrollView.
推荐答案
- 听听键盘的大小变化
- 转换坐标
工作示例:
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
//keyboard observers
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
func keyboardWillChange(notification:NSNotification)
{
print("Keyboard size changed")
if let keyboardSize = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect {
//convert gotten rect
let r = self.view.convert(keyboardSize, from: nil)
//test it
if r.intersects(textView.frame) {
print("intersects!!!")
}
}
}
这篇关于找出UIKeyboard.frame与其他框架何时相交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文