UITableView in UINavigationController gets under Navigation Bar after rotation

前端 未结 8 817
清酒与你
清酒与你 2020-12-21 06:10

This is Portrait:

This is Landscape

I\'ve tried this on rotation with no success:

self.tableView.autoresizesSubviews = YE         


        
相关标签:
8条回答
  • 2020-12-21 06:53

    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?

    0 讨论(0)
  • 2020-12-21 06:53

    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
    
        }
    
    0 讨论(0)
  • 2020-12-21 06:53

    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.

    0 讨论(0)
  • 2020-12-21 06:58

    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.

    0 讨论(0)
  • 2020-12-21 06:59

    Add the following line in the didRotateFromInterfaceOrientation method.

    [self.navigationController.view layoutSubviews];

    I hope this will do.

    0 讨论(0)
  • 2020-12-21 07:02

    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.

    0 讨论(0)
提交回复
热议问题