我在创建消息传递应用程序时遇到麻烦,打开键盘后,我不确定键盘的总大小和框架(是否打开了词典区域)。
我想获得总尺寸和框架
textFieldShouldBeginEditing
代表。
最佳答案
您应该使用UIKeyboardWillChangeFrameNotification。另外,请确保将CGRect转换为正确的视图以供横向使用。
在textFieldShouldBeginEditing
中设置NSNotificationCenter
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
并编写此方法。
- (void)keyboardWillChange:(NSNotification *)notification {
CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; //this is it!
}
在Swift 4中
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_noti:) ), name: NSNotification.Name.UIKeyboardWillChangeFrame , object: nil)
KeyboardWillChange方法
@objc func keyboardWillChange(_noti:NSNotification)
{
let keyBoard = _noti.userInfo
let keyBoardValue = keyBoard![UIKeyboardFrameEndUserInfoKey]
let fram = keyBoardValue as? CGRect // this is frame
}
关于ios - 在textFieldShouldBeginEditing委托(delegate)中获取键盘大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32709032/