简体   繁体   中英

UITextView contentOffset not initialized with CGPointZero on iOS7

I have a nib with a full screen textView. In viewDidLoad ,

self.textView.attributedText = //some text;

Its as simple as this and works fine except for iphone landscape mode.

In landscape mode, when I navigate to this page, contentOffset.y of this textView is not initilized to zero. So, by default the scroll position is at the middle of the content (I expected this to be at the start of the content).

For ipad and iphone portrait mode, scroll position is at the start of the content ( contentOffset.y is zero)

In iOS7, to scroll to the top, you must take the content insets into account. I also found it was necessary to wait for the next runloop iteration, as Altaveron says.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    _textView.contentOffset = CGPointMake(-_textView.contentInset.left, -_textView.contentInset.top);
});

In iOS 7, I was also having the same issue. UITextView's scroll starts in the middle in the landscape view. I have navigationBar at the top of the view controller. To solve this issue, I've set the contentOffset in viewDidLoad as follows:

[self.textView setContentOffset: CGPointMake(0,-200) animated:NO];

I've subtracted 200 to balance with my navigation bar. And it worked for me. Since it is set in viewDidLoad, y offset is not reset to 0 if the user changes orientation.

Hope it helps!

The following workaround helped me. setText calls UITextView setText method.

 [self performSelector:@selector(setText) withObject:nil afterDelay:0.f];

On iOS8 or older version of iOS, using textView.contentOffset = CGPointZero . But from iOS 9 i think we need to update it on viewDidLayoutSubviews()

My similar issue was solved with the following:

textView.contentOffset = CGPointMake(0.0, -50)

On certain devices, the textView would initially be slightly scrolled down. But I needed it to be scrolled to the top.

For some reason, setting it to CGPointZero did not have any affect. However, setting the offset y to a large negative number did the trick.

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