Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automaticallyAdjustsScrollViewInsets doesn't work in embedded UITableViewController with UITableView

When you have a UITableView inside a UIViewController, you have to turn off the automaticallyAdjustsScrollViewInsets flag (either in code or storyboard editor) to get the top space to not avoid the top bar. I now have a child UIViewController using an "embed" seque inside of a child view which contains a UITableView. I am seeing the "space" (the yellow area) yet I cleared all the flags in the embedded UIViewController and even cleared the flag manually in prepareForSeque, yet the space remains. Is there something else one has to do, or is this a bug?

enter image description here

like image 526
ahwulf Avatar asked Feb 11 '15 21:02

ahwulf


2 Answers

Had this issue this morning.

Turns out, you need to uncheck automaticallyAdjustsScrollViewInsets property from parentViewController instead of child when you are using embedded view controller mode like you did.

Here is my UI structure:

ParentViewController has a container view which point to ChildViewController (subclass of UITableViewController).

Uncheck automaticallyAdjustsScrollViewInsets in ChildViewController: doesn't work

Uncheck automaticallyAdjustsScrollViewInsets in ParentViewController: works!

like image 89
yangt Avatar answered Oct 01 '22 17:10

yangt


I have been struggling with this for hours until I realized that the answer by @user3188985 was what was happening, though the solution did not work for me.

This is because I have two contained view controllers that each contain scrollViews, and I want one to adjust, and one not, what to do?


Well if you turn off auto adjustment in the parent controller, then you must manually adjust the one scrollView that you need, e.g. setting contentInset on appropriate cases.


If you leave on auto adjustment in the parent, I have found that a dummy scrollView, with a zero frame, hidden, all scroll properties set to NO, and if it is before your visible/enabled scrollView in the child view controller view hierarchy, then it will receive the auto adjustment, and leave your visible scrollView view alone.

Alternatively, you can just have your contained view controller's top most view be a scrollView with scrolling disabled, and have it contain another scrollView with scrolling enabled, this will achieve the same thing as the "dummy" approach.


What you can see here, is that the auto adjustment flag of only the parent view controller is taken into account, but it's own view controller hierarchy is traversed to find scrollViews to adjust in each contained controller. This is simply unwanted UIKit behavior. It would be nice each child controller's auto adjust setting was checked before deciding to mess with it :/

like image 26
drkibitz Avatar answered Oct 01 '22 15:10

drkibitz