简体   繁体   English

带有NavigationBar的TabBarController上的彩色状态栏无效

[英]Coloured Status Bar on a TabBarController with NavigationBar not working

I've read a lot of posts on here and tried most of the options mentioned, but none fix the issue for me. 我在这里阅读了很多帖子,并尝试了大多数提到的选项,但没有人为我解决问题。 I have an app that is based off of a Tab Bar Controller. 我有一个基于标签栏控制器的应用程序。 Each tab is a UIViewController with a Navigation Bar at the top. 每个选项卡都是一个UIViewController,顶部有一个导航栏。

Adding this code to the AppDelegate gives me an orange coloured Navigation Bar with white text, but a white status bar with Black text. 将此代码添加到AppDelegate为我提供了带有白色文本的橙色导航栏,但是带有黑色文本的白色状态栏。

[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

Reading the answers on various pages suggest adding the following to the View controller: 阅读各种页面上的答案建议将以下内容添加到View控制器:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

Then calling this in View Did Load: 然后在View Did Load中调用它:

[self setNeedsStatusBarAppearanceUpdate];

This gets me a White status bar with White text, how can I now get the status bar to go orange to match my Navigation Bar?? 这让我得到一个带有白色文本的白色状态栏,我现在如何让状态栏变为橙色以匹配我的导航栏?

The solution mentioned on here https://stackoverflow.com/a/19513714/505457 for those using a Navigation Controller doesn't work, I guess thats because my main controller is a Tab Bar Controller. 对于使用导航控制器的人来说,这里提到的解决方案https://stackoverflow.com/a/19513714/505457不起作用,我想这是因为我的主控制器是一个标签栏控制器。

Anyone come across this before? 有没有人遇到过这个? Thanks in advance for any advice / suggestions you may have. 提前感谢您提出的任何建议/建议。 I can provide a sample app if required, but its probably as quick to build one with the Tab Bar template, add a Navigation bar then paste in my code samples. 如果需要,我可以提供一个示例应用程序,但可能使用Tab Bar模板快速构建一个,添加导航栏然后粘贴我的代码示例。

Plasma 等离子体

You can find the statusBar UIVIew by it's name and tint it. 你可以通过它的名字找到statusBar UIVIew给它着色。 Add this method to your AppDelegate.m and call it from didFinishLaunchingWithOptions : 将此方法添加到AppDelegate.m并从didFinishLaunchingWithOptions调用它:

- (void)setStatusBarBackgroundColor:(UIColor *)color {

    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 ...   

    [self setStatusBarBackgroundColor:[UIColor orangeColor]];

 ...

}

Note: There are apps in store that use this method. 注意:商店中有些应用程序使用此方法。 So it is okay with the apple HIG policy. 所以苹果HIG政策没问题。

Well I had almost the same problem since IOS7 , But my quick solution is to change all your views from the first one, I am refering to set up your color bar configuration in the AppDelegate.m file, I recommend to use this free framework Nab Bar Color And Gradient is very easy to use and also you will be available to set a beautiful gradient on all of your views. 好吧,自IOS7以来我遇到了几乎相同的问题,但我的快速解决方案是从第一个改变你的所有视图,我在AppDelegate.m文件中提到设置你的颜色条配置,我建议使用这个免费的框架Nab条形颜色和渐变非常易于使用,您还可以在所有视图上设置漂亮的渐变。

See the examples in the project. 请参阅项目中的示例。

Normally, you shouldn't be adding a UINavigationBar to a UIViewController . 通常,您不应该将UINavigationBar添加到UIViewController Instead, fill your UITabBarController will UINavigationController s. 相反,填写你的UITabBarControllerUINavigationController

    let firstViewController = FirstViewController(nibName: nil, bundle: nil)
    let firstNavigationController = UINavigationController(rootViewController: firstViewController)

    let secondViewController = SecondViewController(nibName: nil, bundle: nil)
    let secondNavigationController = UINavigationController(rootViewController: secondViewController)

    let navigationControllers = [
        firstNavigationController,
        secondNavigationController
    ]

    yourTabBarController.setViewControllers(navigationControllers, animated: false)

The same general process applies if you are using storyboards. 如果您使用故事板,则适用相同的一般过程。

Right so the point BSmith11 made about adding Navigation controllers to a Tab Bar got me thinking. 是的,因此BSmith11关于将导航控制器添加到Tab Bar的观点让我思考。 So I did a bit of Googling and an answer on this page, helps a lot: Tab bar controller inside a uinavigationcontroller 所以我做了一些谷歌搜索并在这个页面上给出了答案,帮助很多: uinavigationcontroller里面的标签栏控制器

By having the TabBar controller as the rootViewController, then inserting a "NavigationController" between that and the normal ViewController allows me to do this: 通过将TabBar控制器作为rootViewController,然后在它和普通的ViewController之间插入一个“NavigationController”允许我这样做:

    //Fix up the Navigation bar tint and set text colours to white
[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor orangeColor]];

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

//Also requires setting 'View controller-based status bar appearance' to NO in .plist
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

Which is exactly what I had before, and that gives me a coloured StatusBar and the exact same colour NavBar. 这正是我之前所拥有的,这给了我一个彩色的StatusBar和完全相同的颜色NavBar。 Now to see if I can add it in to the existing app and not break everything. 现在,看看我是否可以将其添加到现有应用程序中,而不是破坏所有内容。

Plasma 等离子体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM