【问题标题】:Why UIScrollView does not scroll up when UITextView becomes first responder?当 UITextView 成为第一响应者时,为什么 UIScrollView 不会向上滚动?
【发布时间】:2018-02-17 00:53:09
【问题描述】:

UITextView 在用户点击按钮以在UIScrollView 上添加UITextView 时成为第一响应者。实际上包含UITextViewUITextField 的UIView 被添加到UIScrollView 中。当键盘出现时,scrollview 不会向上滚动,但是 UITextField 在成为第一响应者时会向上滚动。我注册UIKeyboardDidShowNotification 向上滚动。下面是我的代码

- (void)keyboardDidShow:(NSNotification *)notification {
        CGRect keyboardEndFrame;
        [(notification.userInfo)[UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

        NSInteger height = CGRectGetHeight(keyboardEndFrame);

        NSInteger lastObj = arrayTotalObjects.count - 1;
        UIView *lastView = [arrayTotalObjects objectAtIndex:lastObj];

        if(!(lastView.frame.origin.y > height))
            return;

        UIEdgeInsets inset = originalInset;
        inset.bottom += height;
        NSLog(@"inset: %f", inset.bottom);

        [UIView animateWithDuration:0.3f animations:^{
            scrlView.contentInset = inset;
        }completion:^(BOOL finished) {
            NSLog(@"scrlView inset bottom: %f", scrlView.contentInset.bottom);
        }];
}

在这种情况下,lastView 将是包含 UITextView 的 UIView。

此代码适用于UITextField,但不适用于UITextView。这怎么可能?是不是因为UITextViewUIScrollView 的子类?

【问题讨论】:

    标签: ios objective-c uiscrollview uitextfield uitextview


    【解决方案1】:

    如果有人看到这个问题,答案设置为UITextView scrolling enabled false

    lawicko 的回答中,我得到了提示。 subclass of UIScrollView.

    对我有用。

    【讨论】:

      【解决方案2】:

      简而言之,您的问题的答案是肯定的,这种行为是由于 UITextView 是 UIScrollView 的子类引起的。您可以找到更多详细信息以及如何禁用它的解决方案,here

      【讨论】:

      • 投反对票的原因是:1. 这不是是/否问题 2. 引用的链接也没有解决方案。
      猜你喜欢
      • 2011-06-02
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      • 2023-03-24
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      相关资源
      最近更新 更多