Scroll to bottom of UITextView erratic in iOS 7

前端 未结 9 1634
无人及你
无人及你 2020-12-01 04:58

The following code will work fine in iOS < 7.0. In iOS 7 the scrolling will be choppy and erratic while the UITextView is updating. I\'m not sure if this is a bug in iO

相关标签:
9条回答
  • 2020-12-01 05:04

    Try this:

    // Don't forget to set textView's delegate 
    -(void)textViewDidChangeSelection:(UITextView *)textView {
        [textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
    }
    
    0 讨论(0)
  • 2020-12-01 05:06

    please try this solution

    -(void) scrollToBottom {
        [textView setContentOffset:CGPointMake(0.0, textView.contentSize.height) animated:YES];
    }
    
    0 讨论(0)
  • 2020-12-01 05:07

    There are two problems in iOS 7 that could explain your problem:

    • The contentOffset is not always up to date in iOS 7.
    • scrollRangeToVisible: will not scroll to an empty line at the end of the text view.

    The solution could be:

    -(void)scrollOutputToBottom {
        CGRect caretRect = [textView caretRectForPosition:textView.endOfDocument];
        [textView scrollRectToVisible:caretRect animated:NO];
    }
    
    0 讨论(0)
  • 2020-12-01 05:08

    This is obviously an iOS 7 bug. Here is a workaround until apple fixes it. The workaround is basically instantiates a UITextView by creating an NSTextStorage and NSLayoutManager from scratch. Apple must have forgotten to initialize something in UITextView initialization method. I filed a bug report and I hope you do too.

    // ios7 bug fix
    // check if the device is running iOS 7.0 or later
    NSString *reqSysVer = @"7.0";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer  options:NSNumericSearch] != NSOrderedAscending);
    
    if (osVersionSupported) {
        NSTextStorage* textStorage = [[NSTextStorage alloc] init];
        NSLayoutManager* layoutManager = [NSLayoutManager new];
        [textStorage addLayoutManager:layoutManager];
        NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];
        [layoutManager addTextContainer:textContainer];
        yourTextView = [[UITextView alloc] initWithFrame:someFrameForYourTextView
                                           textContainer:textContainer];
        // if using ARC, remove these 3 lines
        [textContainer release];
        [layoutManager release];
        [textStorage release];
    }
    else {
        yourTextView = [[UITextView alloc] initWithFrame:someFrameForYourTextView];
    }
    
    0 讨论(0)
  • 2020-12-01 05:16

    This works for me in iOS7.

    -(void) scrollToBottom {
      [textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
      [textView setScrollEnabled:NO];
      [textView setScrollEnabled:YES];
    }
    
    0 讨论(0)
  • 2020-12-01 05:18

    That works for me. Reference: UITextView setText should not jump to top in ios8

    self.textView.layoutManager.allowsNonContiguousLayout = NO;
    self.textView.text = fileContent;
    if(fileContent.length > 1)
    {
        NSRange range = NSMakeRange(self.textView.text.length - 1, 1);
        [self.textView scrollRangeToVisible:range];
    }
    
    0 讨论(0)
提交回复
热议问题