【发布时间】:2015-12-22 16:36:43
【问题描述】:
我正在使用UIKeyboardWillShowNotification 来处理键盘的显示和隐藏。如果我在 iOS8 模拟器/设备上运行它,一切都会完美运行,但它让我在 iOS9 模拟器/设备上感到头疼。在详细说明我的问题之前,我必须补充一点,如果我使用UIKeyboardDidShowNotification,一切都会像魅力一样发挥作用。
问题一:
我有一个UIScrollView,其中包含多个UITextFields。我使用以下代码:
- (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。
有什么我没有看到的,或者这是某种错误?
【问题讨论】:
标签: ios objective-c