UITextView in iOS7 doesn't scroll

UITextView in iOS7 has been really weird. As you type and are entering the last line of your UITextView, the scroll view doesn‘t scroll to the bottom like it should and it causes the text to be "clipped".

The problem is due to iOS 7. In the text view delegate, add this code:
- (void)textViewDidChange:(UITextView *)textView {
    CGRect line = [textView caretRectForPosition:
        textView.selectedTextRange.start];
    CGFloat overflow = line.origin.y + line.size.height
        - ( textView.contentOffset.y + textView.bounds.size.height
        - textView.contentInset.bottom - textView.contentInset.top );
    if ( overflow > 0 ) {
        // We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
        // Scroll caret to visible area
        CGPoint offset = textView.contentOffset;
        offset.y += overflow + 7; // leave 7 pixels margin
        // Cannot animate with setContentOffset:animated: or caret will not appear
        [UIView animateWithDuration:.2 animations:^{
            [textView setContentOffset:offset];
        }];
    }
}


It worked.


UITextView in iOS7 doesn't scroll,布布扣,bubuko.com

UITextView in iOS7 doesn't scroll

上一篇:开源 免费 java CMS - FreeCMS1.9 移动APP生成栏目列表数据


下一篇:学习java随笔第九篇:java异常处理