2

I have a view controller that consists of two child UIContainerViews, one of which is fixed-width, and the other which dynamically adjusts its width based on portrait vs landscape mode. Both contain UITableViews.

For some reason, if the screen loads in portrait orientation it renders fine. However if it loads in landscape and then rotates to portrait, the second (dynamic) table gets all screwy and thinks it's wider than it is/should be, letting you scroll it horizontally which it should not be doing.

The cells of the tableView all size properly and stick to the left side of this "too wide" tableView. However, if I color the background of the tableview pink, I can see the whole thing is extending into this too wide zone.

It's baffling me what's going on here. Shouldn't autolayout be the same regardless of which orientation the view controller was loaded as?

If I log the widths in a viewDidRotate... handler, everything appears to have the correct width, yet it's still rendering in this bizarre way.

Is there perhaps I way I can just force the container view to re-lay it self out?

Update: It is the contentSize of the tableView that is getting messed up. All the other widths are correct when logged, but the contentSize.width is way off. The good news is I can just manually set this back to what it should be and everything works great! However it doesn't answer the question as to why it's happening in the first place or what I'm doing wrong (if anything).

Here's a screenshot:

enter image description here

mkubilayk
  • 2,477
  • 3
  • 20
  • 27
devios1
  • 36,899
  • 45
  • 162
  • 260

1 Answers1

2

In order to force a UITableView to (re)calculate its content size, you may use layoutIfNeeded method. It traverses through the view hierarchy and lays out the subviews immediately, so content size is set properly.

Instead of setting the size manually, call this method for your table view.

mkubilayk
  • 2,477
  • 3
  • 20
  • 27