How to add custom navigation bar to a full-screen

2019-02-20 08:53发布

问题:

In an app I'm developing (Xcode 5, >=iOS 7 auto-layout), I push a modal view controller. I want the modal view controller to have a navigation bar, so I add one, and add a constraint that positions it with it's top aligned with the top layout guide, so it is placed just under the status bar.

(I'm using my own navigation bar because I want to take advantage of the system's management of the edit button and add button nav bar items. )

The standard navigation bar has a very subtle light gray tint to it.

With navigation controllers, the system somehow tints the status bar, or extends the navigation bar up so that it colors the status bar the same color as the navigation bar, and the effect is as if the status bar is part of a taller navigation bar.

I don't know how to get this effect with a navigation bar I add to a window that is not managed by a navigation controller. I have vague memories of there being some sort of property I need to set that tells the status bar to adopt the tint color of the navigation bar, but despite searching and searching, I can't find it. I could put a view under the status bar and fiddle with it until it's color matches that of the navigation bar, but that's a hack, and if in the future the look of standard navigation bars changes, this approach would stop looking right.

Does anybody know the secret of this?

回答1:

Did you try adding the NavigationBar Delegate

@interface MyClass : UIViewController <UINavigationBarDelegate>

And the following method?

- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar {
    return UIBarPositionTopAttached;
}

And remember to add a constraint of 20 from the top.