bug in UITableView layout after orientation change

19,986

Solution 1

I think this is a bug; if you convert the project away from auto layout and set appropriate resizing masks, everything works just fine. You should file a bug, particularly since you have a simple sample project that reproduces it nicely.

What is happening in your case is as you suspected - the scroll view's content view remains at the landscape width, even though the view itself has resized to portrait, so you suddenly get the ability to horizontally drag.

Luckily there are fairly simple workarounds. I experimented with various combinations of setNeedsLayout or setNeedsUpdateConstraints without success, but you can implement one of these two solutions:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

Or,

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    CGSize contentSize = self.tableView.contentSize;
    contentSize.width = self.tableView.bounds.size.width;
    self.tableView.contentSize = contentSize;
}

Solution 2

I have found that the answers above work most of the time but not all of the time. The most robust solution is to subclass UITableView and set the contentSize within layoutSubviews:

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGSize contentSize = self.contentSize;
    contentSize.width  = self.bounds.size.width;
    self.contentSize   = contentSize;
}
Share:
19,986

Related videos on Youtube

ConfusedNoob
Author by

ConfusedNoob

Updated on June 19, 2022

Comments

  • ConfusedNoob
    ConfusedNoob about 2 years

    I have an auto-layout (constraint) based application and noticed that there is a problem with a tableview after an orientation change. The repro steps are as follows (I have a very basic repro app - with a tableview and an add button that adds a new item).

    1. Rotate your application to Landscape mode so the tableview is now landscape.
    2. Add an item to the tableview see 1 below
    3. Rotate back to Portrait, the tableview will now float on horizontal pan (as though the scroll viewer content size is landscape), see [2]

    1 Code to add

    - (IBAction)onAdd:(id)sender {
        count ++;
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
    }
    

    [2] Floating table view

    enter image description here

    Any ideas on how to work around this problem? Also, how can I tell if this is known issue or something I should report to Apple?

    • Max Steinmeyer
      Max Steinmeyer over 11 years
      Can you post your example app?
    • runmad
      runmad over 11 years
      Are you using a UITableViewController or a UITableView as a subview in a UIViewController? Might be an issue with the layout of the tableview if it's a subview.
    • ConfusedNoob
      ConfusedNoob over 11 years
      It's a UITableView as a subview of UIViewController
    • jmstone617
      jmstone617 over 11 years
      Show your autorotation code.
    • ConfusedNoob
      ConfusedNoob over 11 years
      I don't have any 'autorotation' code. The table view just uses autolayout in IB. It works fine unless you add a row in the scenario described above.
    • ConfusedNoob
      ConfusedNoob over 11 years
      Note, this problem does not repro in a full UITableViewController, only a subview UITableView
    • ConfusedNoob
      ConfusedNoob over 11 years
    • Anton
      Anton over 11 years
      I think you may want to submit a bug report!
  • Sukhrob
    Sukhrob about 11 years
    It does not work if I use one container view controller with segmented control and child view controllers.
  • Alexander Tkachenko
    Alexander Tkachenko over 10 years
    Thanks for your help! but you probably forgot to call [super didRotateFromInterfaceOrientation]
  • Cullen SUN
    Cullen SUN over 9 years
    In iOS 6, I had a problem with UITableView's behaviour after change its constraints. beginUpdates & endUpdates saved me. Thanks a lot