I have a modal screen with a Page View. The Page View loads the pages using storyboard IDs. All pages are the same. Each page has a UITextView with some text and its scrollable. At viewDidLoad the text is set. When the text is set, it automatically scrolls to bottom, so at viewDidAppear I invoke [self.textField setContentOffset:CGPointZero animated:animated];.
This works fine, except for the first page. Due to a bug, in viewDidLayoutSubviews I need toggle scrollEnabled for the UITextView. However, when the first page is loaded, viewDidAppear is for some reason called prior to viewLayoutSubviews, which has the side-effect of scrolling the UITextView to bottom, probably due to the toggling. If I scroll past the first page and then return to first page, it works as expected (then the calling order is the same as for the other pages).
Some other things I've tried:
- Setting the text in
async_dispatchon main queue inviewDidLoad. - Calling
[self.textField scrollRangeToVisible:NSMakeRange(0, 0)];atviewDidLayoutSubviews - Various combinations of
scrollRangeToVisibleandsetContentOffset. - Setting
self.automaticallyAdjustsScrollViewInsets = NO;inviewDidLoad. - Trying to scroll the view after setting the text (using either of the above methods)
Anyone have an idea on how to fix this?
Here is the code for the view controller:
- (void)viewDidLoad {
[super viewDidLoad];
// Get help string
NSMutableAttributedString *attributedString = <fetch text from external source>
// Set text
self.textField.attributedText = attributedString;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.textField setContentOffset:CGPointZero animated:animated];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.textField.scrollEnabled = NO;
self.textField.scrollEnabled = YES;
}