Search code examples
objective-cxcode5

How does UIViewController's automaticallyAdjustsScrollViewInsets work anyway?


self.automaticallyAdjustsScrollViewInsets = YES;

I set that to yes. I think it's yes by default.

Then when I check the inset this is what I got: NSStringFromUIEdgeInsets(self.tableView.contentInset): {0, 0, 0, 0}

I thought it should be set up in a way that incorporate the navigationBar and statusbar. So what happened?


Solution

  • From what I know

    automaticallyAdjustsScrollViewInsets

    allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar

    • works on the view which is direct subview (someone says it must be the first subview) of the View Controller 's view
    • this view must be a ScrollView subclass
    • the View Controller is embedded in Navigation Controller or TabBar Controller
    • edgesForExtendedLayout is UIEdgeRectTop (if you want the ScrollView subclass view to be behind the top bar), UIEdgeRectBottom (if you want the ScrollView subclass view to be behind the bottom bar)

    The code below replicates what automaticallyAdjustsScrollViewInsets does when edgesForExtendedLayout = UIEdgeRectTop

    self.scrollView.contentInset = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, 0, 0);
    self.scrollView.contentOffset = CGPointMake(0, -self.topLayoutGuide.length);