繁体   English   中英

当 UITextView 成为第一响应者时,为什么 UIScrollView 不向上滚动?

[英]Why UIScrollView does not scroll up when UITextView becomes first responder?

当用户点击按钮在UIScrollView上添加UITextView时, 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子类吗?

如果有人看到这个问题,答案是 set UITextView scrolling enabled false

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

它对我有用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM