UITableView layout error after orientation change

I have an application with automatic layout (restriction) and noticed that after changing the orientation there is a problem with the table view. The playback steps are listed below (I have a very basic playback application - with a table view and an add button that adds a new item).

  • Rotate the application in landscape mode so that it is now viewed in a table.
  • Add item to view table 1 below
  • Go back to the "Portrait" position, now the table will float in a horizontal panorama (as if the size of the contents for scrolling was 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 get around this? Also, how can I find out if this is a known issue or something that I should tell Apple?

+18
ios objective-c xcode uitableview autolayout
Jan 13 '13 at 19:03
source share
2 answers

I think this is a mistake; if you convert the project away from the automatic layout and set the appropriate masks for resizing, everything works fine. You should indicate a mistake, especially because you have a simple sample project that reproduces it beautifully.

What happens in your case, just as you suspected - viewing the contents of the scroll view remains at the width of the landscape, even if the view itself has changed to a portrait, so that you suddenly get the option of horizontal drag and drop.

Fortunately, 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; } 
+30
Jan 20 '13 at 20:38
source share

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

 - (void)layoutSubviews { [super layoutSubviews]; CGSize contentSize = self.contentSize; contentSize.width = self.bounds.size.width; self.contentSize = contentSize; } 
+3
Jun 15 '13 at 22:33
source share



All Articles