Scroll UITextView To Bottom

后端 未结 10 1668
误落风尘
误落风尘 2020-12-08 07:26

I am making a an app that has a UITextView and a button.

When I click the button some text will add in the UITextView.

But when c

相关标签:
10条回答
  • 2020-12-08 08:01

    Swift 5

    extension UITextView {
        func simple_scrollToBottom() {
            let textCount: Int = text.count
            guard textCount >= 1 else { return }
            scrollRangeToVisible(NSRange(location: textCount - 1, length: 1))
        }
    }
    
    // Usage
    textView.simple_scrollToBottom()
    
    0 讨论(0)
  • 2020-12-08 08:04

    Make a range, specifying encoding, to the last character, then scroll to that range Something other than utf8 might be appropriate depending on your content

    let range = NSMakeRange(self.OutputTextView.text.lengthOfBytes(using: String.Encoding.utf8), 0);
    self.OutputTextView.scrollRangeToVisible(range);
    
    0 讨论(0)
  • 2020-12-08 08:05

    Try this if you have problem on iOS 7 or above. See this SO answer.

    - (void)scrollTextViewToBottom:(UITextView *)textView {
        NSRange range = NSMakeRange(textView.text.length, 0);
        [textView scrollRangeToVisible:range];
        // an iOS bug, see https://stackoverflow.com/a/20989956/971070
        [textView setScrollEnabled:NO];
        [textView setScrollEnabled:YES];
    }
    
    0 讨论(0)
  • 2020-12-08 08:05

    This works for me! :D

    CGPoint bottomOffset = CGPointMake(0, self.textView.contentSize.height - self.textView.bounds.size.height);
    [self.description1 setContentOffset:bottomOffset animated:YES];
    
    0 讨论(0)
提交回复
热议问题