UITableView in UINavigationController gets under Navigation Bar after rotation

爷,独闯天下 提交于 2019-11-30 09:27:48

问题


This is Portrait:

This is Landscape

I've tried this on rotation with no success:

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

回答1:


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.




回答2:


When using a custom view controller as root, which manages other view controllers itself, then it is responsible to forward all rotation events to the currently active sub-controller (if I may call it like that). These events are:

– willRotateToInterfaceOrientation:duration:

– willAnimateRotationToInterfaceOrientation:duration:

– didRotateFromInterfaceOrientation:

– willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:

– didAnimateFirstHalfOfRotationToInterfaceOrientation:

– willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:

So in the case of a navigation controller, managed by your custom root controller, forwarding – willAnimateRotationToInterfaceOrientation:duration: to the navigation controller fixes the issue with the wrong navigation bar height when rotating.




回答3:


Add the following line in the didRotateFromInterfaceOrientation method.

[self.navigationController.view layoutSubviews];

I hope this will do.




回答4:


I have exactly the same issue, and @jstevenco led me to a really strange solution. My views also auto resize on rotation, but the tableview doesn't seem to understand there is a navigationcontroller sitting there:

Within the tableviewcontroller, add the following:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
 CGRect frame = navBar.frame;
if (fromInterfaceOrientation == UIInterfaceOrientationPortrait || fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    frame.size.height = 31.9;
} else {
    frame.size.height = 44.1;
}
navBar.frame = frame;  

}

The 31.9 and 44.1 are very important strangly! Leaving these are 32 and 44, there is no resizing to do, so it seems to ignore it. Worked for me, hope it helps someone else.




回答5:


I looks like the table view is scrolled down a little after the orientation change.

Have you tried calling scrollToRowAtIndexPath:atScrollPosition:animated: and scrolling to the top-most row?




回答6:


This problem can occur if you've called "willAnimateRotationToInterfaceOrientation:" in your navigation controller, but forgotten to call super from within the method.

You should have something like this:

        - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation];
// Your code here

    }



回答7:


One thing to look out for in these situations is the orientation of the Status Bar if you are changing it manually. If the status bar is not hidden and you change the frame of your view while the status bar is in the wrong orientation (in your code, maybe you're updating the orientation of the status bar just after the frame change), it will auto resize the navigation bar wrongly, even if you haven't got auto resize on.




回答8:


Your UINavigationController needs to get notified when the device orientation changes. This doesn't happen if you only add the view of a UINavigationController to another UIView.

Starting with iOS 5, you can simply call addChildViewController: on the parent UIViewController, which will ensure that orientation changes are propagated to this child view controller.



来源:https://stackoverflow.com/questions/1882107/uitableview-in-uinavigationcontroller-gets-under-navigation-bar-after-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!