简体   繁体   中英

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

UITextView becomes first responder when user taps on a button to add a UITextView on UIScrollView . Actually UIView which contains UITextView or UITextField is added in UIScrollView . When keyboard appears, scrollview does not scroll up, however UITextField makes it scrolling up when it becomes first responder. I register UIKeyboardDidShowNotification to scroll up. Below is my code

- (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);
        }];
}

In this case lastView will be UIView containing UITextView.

This code works for UITextField but not for UITextView . How is that possible? Is it because UITextView is subclass of UIScrollView ?

If someone see this question, The answer is set UITextView scrolling enabled false

In lawicko 's answer, I got a hint. subclass of UIScrollView .

It works to me.

In short, the answer to your question is yes, this behavior is caused by UITextView being a subclass of UIScrollView. You can find more details, as well as the solution how to disable it, here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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