我正在使用UIKeyboardWillShowNotification
來處理顯示和隱藏鍵盤。如果我在iOS8模擬器/設備上運行它,一切都很完美,但它讓我在iOS9模擬器/設備上頭疼。在詳細討論我的問題之前,我必須補充一點,如果我使用UIKeyboardDidShowNotification
,那麼所有的東西都像魅力一樣。iOS9中的UIKeyboardWillShowNotification問題
問題1:
我有一個UIScrollView
,其包含多個UITextField
第我使用下面的代碼:
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary *info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
NSNumber *rate = aNotification.userInfo[UIKeyboardAnimationDurationUserInfoKey];
[UIView animateWithDuration:rate.floatValue animations:^{
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
} completion:^(BOOL finished) {
CGPoint scrollPoint = CGPointMake(0.0, kbSize.height);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
當我使用iOS9設備上的驗證碼,滾動視圖不會滾動到我希望它的地步,只是這是第一個響應者在文本字段下方滾動反之亦然。但是,當我點擊另一個文本字段後,它會滾動到所需的位置。
問題2:
我有一個UICollectionView
,其中包含一個UITextField
,並使用流佈局。該代碼是與上述相同,除了contentOffset,爲此,我使用的設定:
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
當我使用iOS9裝置上該代碼時,contentInset
被設定爲正好兩倍大小是應該的。它看起來好像被設置了兩次 - 一次自動並且因爲我的代碼而再次設置,儘管設置爲self.automaticallyAdjustsScrollViewInsets = NO;
。還有一件事要補充我的第二個問題 - 如果我在代碼中忽略了contentInset
的設置,它會在iOS9設備上設置爲正確的值,但在iOS8設備上當然保持爲0.0。
有沒有我沒有看到或是這種某種錯誤?