【发布时间】:2014-11-13 05:02:31
【问题描述】:
我有几个 UITextFields 在弹出时会被键盘隐藏,所以我实现了 UIKeyboardWillShowNotification 和 UIKeyboardWillHideNotification 并将它们发送到各自的方法:
- (void)keyboardWillShow:(NSNotification*)notification
{
CGFloat height = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
keyBoardHeight = height;
}
- (void)keyboardWillHide:(NSNotification*)notification
{
CGFloat height = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
keyBoardHeight = height;
}
然后我将我的文本字段委托给此方法:
//TextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
float keyboardOffset = [self offsetForKeyboard:textField.frame.origin.y withHeight:textField.frame.size.height];
NSLog(@"%f, %f", keyBoardHeight, keyboardOffset);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
self.view.frame = [Global rect:self.view.frame withNewY:-[self offsetForKeyboard:textField.frame.origin.y withHeight:textField.frame.size.height]];
[UIView commitAnimations];
}
代码运行正常,但是当我运行代码时,keyboardWillShow: 方法在textFieldDidBeginEditing: 方法之后运行。正因为如此,keyboardHeight 在代码第一次运行时被设置为 0,所以我计算的偏移量是很远的。
如何使keyboardWillShow: 方法在textFieldDidBeginEditing: 方法之前运行?我可以使用其他委托方法吗?
【问题讨论】:
-
@rmaddy 是的,这就是我查找的问题,它激发了我为任何文本字段编写一个包罗万象的内容。不过我想通了。谢谢您的帮助! :)
标签: ios objective-c delegates uitextfield uikeyboard