【问题标题】:UIKeyboardWillShowNotification problems in iOS9iOS9 中的 KeyboardWillShowNotification 问题
【发布时间】: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


    【解决方案1】:

    我找到了可行的解决方案,即分派到主队列。我还删除了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];
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 2015-12-16
      • 2015-12-22
      • 1970-01-01
      相关资源
      最近更新 更多