在iOS 5 iPad支持3種不同的鍵盤(普通,拆分,上移)。以前,當出現鍵盤時,控制器將通過KeyboardDidShowNotification進行通知。在這裏,如果我們有任何通過鍵盤隱藏的UI元素將設置偏移量並向上推動元素(通過使用滾動視圖)。在iOS 5中,我們必須根據鍵盤的類型進行處理。我們如何知道鍵盤類型。我們可以爲新的鍵盤類型做些什麼?如何在iOS5中顯示鍵盤隱藏的UIElements?
謝謝, durai。
在iOS 5 iPad支持3種不同的鍵盤(普通,拆分,上移)。以前,當出現鍵盤時,控制器將通過KeyboardDidShowNotification進行通知。在這裏,如果我們有任何通過鍵盤隱藏的UI元素將設置偏移量並向上推動元素(通過使用滾動視圖)。在iOS 5中,我們必須根據鍵盤的類型進行處理。我們如何知道鍵盤類型。我們可以爲新的鍵盤類型做些什麼?如何在iOS5中顯示鍵盤隱藏的UIElements?
謝謝, durai。
如果您在UIKeyboardWillShowNotification
或UIKeyboardWillHideNotification
反應,你應該罰款當「正常模式」顯示的鍵盤,他們只會被髮送。如果使用者拉動其向上或拆分它,您將收到一個UIKeyboardWillHideNotification
(奇怪的行爲但蘋果的唯一選擇,使其向後兼容iOS 4的應用程序)
兼容。如果要滾動鍵盤下面自動隱藏的TextView或文本框元素時,點擊編輯文本元素上,下面的代碼將有助於您的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];
}