3

I'm stuck. I have a slightly complicated hierarchy of views. One UITabBarController to rule three UINavigationControllers. In each NavigationController I has a root UITableViewController that spaws others of its kind. All are subclassed. Everything was working yesterday, and after adjusting some scrollview insets and switching to blocks, I have a problem.

When I push another TableViewController onto the navigation stack everything is ok. But, when I pop that TableViewController and log the contentSize of the root TableViewController

viewWillAppear           contentSize: {320, 15063.414}
viewWillLayoutSubviews   contentSize: {0, 0}
viewDidLayoutSubviews    contentSize: {0, 0}
viewDidAppear            contentSize: {0, 0}

This only happens when popping from my custom TableViewController to another TableViewController of the same kind. Any idea what's going on (presumably in viewDidLayoutSubviews)?

I am suspecting something might be up with the TabBarController or NavigationController but I'm still drawing a blank.

I've updated what was logged to include viewWillLayoutSubviews and viewDidLayoutSubviews. It appears everything is zeroed out before AutoLayout even gets its hands on it. Is that assumption correct?

Edit

The same patter of setting content size properly and then setting it to zero also happens when I push a view controller on top. Then when I pop it off, it sets the proper contentSize and then removes it.

I've come up with a workaround. I cache my cell heights so in viewWillLayoutSubviews I set the contentSize properly. The problem still persists if I present a view modally because viewWillLayoutSubviews isn't called. Something is weird still.

Shawn Throop
  • 1,239
  • 1
  • 11
  • 28
  • Any time I see weird layout behavior I can't explain, I go looking for code that accesses UI components from a thread other than the main thread. You said you "switched to blocks;" perhaps you have UI-manipulating code in those blocks? – NathanAldenSr Mar 20 '14 at 14:14
  • I've tried to be very careful about making completions blocks execute on the main thread. I'll check again though. Is it bad to call a method with a completion block from within any of the viewDidSomething? – Shawn Throop Mar 20 '14 at 14:23
  • It's not bad, but to be safe I always use the `dispatch_async(dispatch_get_main_queue(), ^{});` boilerplate from within blocks and from within receivers where I am not sure what thread is calling them (such as `NSNotificationCenter` selectors). – NathanAldenSr Mar 20 '14 at 14:26
  • I've gone through every method I have and they're all executing on the main thread. I've also removed all code that touches scrollViewInsets and any custom pop animations. still nothing... – Shawn Throop Mar 20 '14 at 19:11
  • Although a different setup, I think I have the same problem : http://stackoverflow.com/questions/27090751/ios-uiscrollview-uitableview-contentoffset-gets-reset-when-pushviewcontrollerani . Have you found a way to solve this properly yet or at least understood the cause of it ? What I've found to work for me is to set the contentSize of the table inside `viewWillLayoutSubviews` but it does not feel right... – Petar Jul 15 '15 at 08:21

1 Answers1

0

You said "and after adjusting some scrollview insets". Keep in mind that x,y references of scrollview differs from main UIWindow (screen) or (UIView) x,y coordinates. Also get rid off AutoLayout or programatically set frame content to desired x,y position, width and height.

Check the code in :

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}
Chris
  • 5,494
  • 2
  • 27
  • 53
ares777
  • 3,422
  • 1
  • 20
  • 22
  • The frame and bounds of the tableView is set appropriately. Just the contentSize is 0,0. I don't understand what I should check in viewWillLayoutSubviews, I have what you have written plus a logging statement. – Shawn Throop Mar 20 '14 at 15:14