5
votes

I've got a UINavigationController with a UIViewController showing a navigation bar.

I've specifically set [self setAutomaticallyAdjustsScrollViewInsets:YES];, though this should be YES by default anyway.

I add a UIScrollView:

self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.scrollView];

However, the UIScrollView's contentInset is {0, 0, 0, 0}, so I am wondering why it doesn't inherit the content inset from the view controller as the documentation states it would.

If I add subviews to the scroll view and set their Y to be self.scrollView.contentInset.top they don't appear below the navigation bar, which is what I am expected.

What am I doing wrong here? It works fine if you start out with a UITableViewController since it correctly inherits the contentInset from the view controller.

Thanks!

2
The contentInset (and contentOffset) adjustment will only be seen after the view is on screen. Can you confirm that they are still not being set in viewDidAppear or later? - Jason Moore
Ah, that might be there issue here, that I am trying to set it in loadView - runmad
Ah, definitely wouldn't recommend doing more in loadView than creating and assigning something to self.view. - Jason Moore
@jefflcy87 Wait, so I have to set self.view = scrollView? - runmad
@runmad My comment previously may have been misleading. What I mean is your view hierarchy must be like this. View (self.view) --->ScrollView --->Other views If you have anything in between self.view and your scrollView [setAutomaticallyAdjustsScrollViewInsets:] will not work i.e. View -->Other views (UIButton, UILabel, UIView etc) -->ScrollView - JLau_cy

2 Answers

-1
votes
self.view = self.scrollView;

Works fine for me. Tested with UIWebview:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view = self.webView;
}
-1
votes

It adjusts the scroll view bounds, not the content insets.