UITableView in UINavigationController gets under Navigation Bar after rotation

Jorge picture Jorge · Dec 10, 2009 · Viewed 9.1k times · Source

This is Portrait:

alt text

This is Landscape

alt text

I've tried this on rotation with no success:

self.tableView.autoresizesSubviews = YES;
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

Answer

jstevenco picture jstevenco · May 19, 2010

I ran into this problem recently. In my case, it was because I was performing rotations of a navigation controller that was not the root controller of the application window. I was therefore using the rotate notification listener/affine transform/bounds adjustment approach to changing the layout of the navigation controller. This worked all right but produced the results described here. It took me a while to notice that when I rotated to landscape the nav bar's height was not being correctly resized (i.e., from 44 to 32 pixels, the framework's reduced height for landscape mode). The root view controller of my app was responding to willRotateToInterfaceOrientation and didRotateFromInterfaceOrientation, however. That view controller also already knew about the associated navigation controller. I remedied the problem by adding the following code to the willRotateToInterfaceOrientation method:

CGRect frame = self.navViewController.navigationBar.frame;
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    frame.size.height = 44;
} else {
    frame.size.height = 32;
}
self.navViewController.navigationBar.frame = frame;

Hope this saves somebody some time in a similar situation.