Adding a UINavigationController as a subview of UIView

前端 未结 5 606
南方客
南方客 2020-12-06 01:39

I\'m trying to display a UILabel on top of a UINavigationController. The problem is that when I add the UILabel as a subview of UIWindow it will not automatically rotate sin

相关标签:
5条回答
  • 2020-12-06 02:19

    Using this code seems to work:

    nav.view.frame = CGRectMake(nav.view.frame.origin.x, nav.view.frame.origin.y - 20,
                              nav.view.frame.size.width, nav.view.frame.size.height);
    

    I did this before adding the navigation controller as a subview. Using the [UIApplication sharedApplication].statusBarFrame instead of the hard coded 20 would probably be a good idea too.

    I'm not sure if it's the best way to do it though.

    0 讨论(0)
  • 2020-12-06 02:20

    Swift 5: add the following line in the viewDidLoad() of the root view controller of the UINavigationController.

    self.edgesForExtendedLayout = [.top, .bottom]
    
    0 讨论(0)
  • 2020-12-06 02:25

    I had this same problem actually but managed to fix it.

    I noticed that my view controller's view had the correct frame, but the view controller's navigation bar did not (it had a frame origin of (0,20) ).

    Insert this into the view's controller that is the superview of the navigation controller:

    - (void) viewDidAppear:(BOOL)animated {
        if (navigationController.navigationBar.frame.origin.y != 0) {
            [[navigationController view] removeFromSuperview];
            [[self view] addSubview:navigationController.view];
        }
    }
    
    0 讨论(0)
  • 2020-12-06 02:26

    If you want a frame representing the available content area, then you should just use: [[UIScreen mainScreen] applicationFrame]. Of course, this restricts your top-level view controller so that it can only be top level. So still kind of dodgy, but less so.

    0 讨论(0)
  • 2020-12-06 02:36

    Why don't you use App Frame instead of adding some values to origins? I mean using:

    CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
    

    as a reference frame, and do something like this:

    nav.view.frame = CGRectMake(appFrame.origin.x, appFrame.origin.y, ...
    

    This one worked for me.

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