How to add custom navigation bar to a full-screen VC in iOS 7 and make it tint the status bar to match?

旧巷老猫 提交于 2019-12-19 11:31:05

问题


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.



来源:https://stackoverflow.com/questions/20668583/how-to-add-custom-navigation-bar-to-a-full-screen-vc-in-ios-7-and-make-it-tint-t

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