繁体   English   中英

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

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

我在这里阅读了很多帖子,并尝试了大多数提到的选项,但没有人为我解决问题。 我有一个基于标签栏控制器的应用程序。 每个选项卡都是一个UIViewController,顶部有一个导航栏。

将此代码添加到AppDelegate为我提供了带有白色文本的橙色导航栏,但是带有黑色文本的白色状态栏。

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

阅读各种页面上的答案建议将以下内容添加到View控制器:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

然后在View Did Load中调用它:

[self setNeedsStatusBarAppearanceUpdate];

这让我得到一个带有白色文本的白色状态栏,我现在如何让状态栏变为橙色以匹配我的导航栏?

对于使用导航控制器的人来说,这里提到的解决方案https://stackoverflow.com/a/19513714/505457不起作用,我想这是因为我的主控制器是一个标签栏控制器。

有没有人遇到过这个? 提前感谢您提出的任何建议/建议。 如果需要,我可以提供一个示例应用程序,但可能使用Tab Bar模板快速构建一个,添加导航栏然后粘贴我的代码示例。

等离子体

你可以通过它的名字找到statusBar UIVIew给它着色。 将此方法添加到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]];

 ...

}

注意:商店中有些应用程序使用此方法。 所以苹果HIG政策没问题。

好吧,自IOS7以来我遇到了几乎相同的问题,但我的快速解决方案是从第一个改变你的所有视图,我在AppDelegate.m文件中提到设置你的颜色条配置,我建议使用这个免费的框架Nab条形颜色和渐变非常易于使用,您还可以在所有视图上设置漂亮的渐变。

请参阅项目中的示例。

通常,您不应该将UINavigationBar添加到UIViewController 相反,填写你的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)

如果您使用故事板,则适用相同的一般过程。

是的,因此BSmith11关于将导航控制器添加到Tab Bar的观点让我思考。 所以我做了一些谷歌搜索并在这个页面上给出了答案,帮助很多: uinavigationcontroller里面的标签栏控制器

通过将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];

这正是我之前所拥有的,这给了我一个彩色的StatusBar和完全相同的颜色NavBar。 现在,看看我是否可以将其添加到现有应用程序中,而不是破坏所有内容。

等离子体

暂无
暂无

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

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