iOS9中的UIKeyboardWillShowNotification问题

时间:2015-12-22 16:36:43

标签: ios objective-c

我正在使用UIKeyboardWillShowNotification来处理键盘的显示和隐藏。如果我在iOS8模拟器/设备上运行它,一切都很完美,但它让我在iOS9模拟器/设备上头疼。在我详细讨论我的问题之前,我必须补充一点,如果我使用UIKeyboardDidShowNotification,一切都像魅力一样。

问题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。

我有没有看到或者这是某种错误?

1 个答案:

答案 0 :(得分:0)

我找到了可行的解决方案,即调度到主队列。我还删除了UIView animateWithDuration: completion:方法,因此代码现在看起来像这样:

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSDictionary *info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);

    dispatch_async(dispatch_get_main_queue(), ^{
        self.collectionView.contentInset = contentInsets;
        self.collectionView.scrollIndicatorInsets = contentInsets;

        NSIndexPath *path = [NSIndexPath indexPathForItem:2 inSection:2];
        [self.collectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
    });
}