如何在iOS5中显示键盘隐藏的UIElements?
在iOS 5中,iPad支持3种不同的键盘(普通、拆分、上移)。以前,当键盘出现时,控制器将通过键盘显示通知。在这里,如果我们有任何被键盘隐藏的UI元素,将设置一个偏移并向上推这些元素(通过使用滚动视图)。在iOS 5中,我们必须根据键盘的类型进行处理。我们如何知道键盘类型。我们能为新的键盘类型做些什么 谢谢,如何在iOS5中显示键盘隐藏的UIElements?,ios5,uielement,keypad,Ios5,Uielement,Keypad,在iOS 5中,iPad支持3种不同的键盘(普通、拆分、上移)。以前,当键盘出现时,控制器将通过键盘显示通知。在这里,如果我们有任何被键盘隐藏的UI元素,将设置一个偏移并向上推这些元素(通过使用滚动视图)。在iOS 5中,我们必须根据键盘的类型进行处理。我们如何知道键盘类型。我们能为新的键盘类型做些什么 谢谢, durai。如果您在UIKeyboardWillShowNotification或UIKeyboardWillHideNotification上做出反应,您应该没有问题,因为只有在键盘显
durai。如果您在
UIKeyboardWillShowNotification
或UIKeyboardWillHideNotification
上做出反应,您应该没有问题,因为只有在键盘显示为“正常模式”时才会发送它们。。如果用户将其拉出或拆分,您将收到一个UIKeyboardWillHideNotification
(奇怪的行为,但苹果公司唯一的选择是使其与iOS 4应用程序向后兼容)如果您想在点击编辑文本元素时自动滚动键盘下方隐藏的文本视图或文本字段元素,以下代码将帮助您处理ios5:
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_scrollView.contentInset = contentInsets;
_scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:_activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrollView.contentInset = contentInsets;
_scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
_activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
_activeField = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self registerForKeyboardNotifications];
}