iPhone: Move UINavigationBar down?

2019-03-02 12:37发布

问题:

I'm trying to position the navigation bar below the top of the screen.

I've tried the following in viewWillAppear:

self.navigationController.view.frame = CGRectMake(0.0, 54.0, 320.0, 426.0);
self.navigationController.navigationBar.frame = CGRectMake(0.0, 0.0, 320.0, 44.0);

The problem is it only seems to work the very first time the view is displayed. If you go to another tab, and back, the navigation bar is at the top of the screen. This also happens when the iPhone is rotated.

Should I be trying to adjust the navigation controller frame, or should I instead try to add the navigation controller to another view that is below the top of the screen?

回答1:

Although late, I hope this can still help someone else out. I used Stephens code inside viewDidAppear:animated and the navigation bar stays put upon traveling back to the tab. This allowed me to use a logo above the navigation bar. I used this inside my viewController.

    - (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.navigationController.view.frame = CGRectMake(0.0, 100.0, 320.0, 426.0); 
self.navigationController.navigationBar.frame = CGRectMake(0.0, 0.0, 320.0, 44.0); 
[self loadData];
    }


回答2:

I can change the frame by overriding the frame property in viewDidAppear of my custom navigation controller. I also add the navigation controller view to another view that is a subview of the window and make same clipping. Pay attention because in this way you're going to loose the standard calls to every view controllers that will be attached to the navigation controllers. I don't remember if I also used a custom view for my view controllers: maybe yes and with didMoveToSuperview or didMoveToWindow overrided just to emulate the default behaviour. Hope it helps and forgive my english.