在iOS7中自定义navigationBar - 标题颜色不起作用

Liv*_*oy7 29 objective-c uinavigationbar uinavigationcontroller ios7 swift

我试图在iOS7中自定义导航控制器的navigationBar,标题颜色不会改变.我正在做以下事情:

    [navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:46.0f/256.0f green:46.0f/256.0f blue:46.0f/256.0f alpha:1]];
    [navigationController.navigationBar setTranslucent:NO];
    [navigationController.navigationBar setTitleTextAttributes:@{[UIColor whiteColor]:UITextAttributeTextColor}];
    [self presentViewController:navigationController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

navigationBar半透明关闭并且很暗,但标题也保持黑暗.我也尝试创建一个自定义标签,并将其设置为标题视图,运气不佳.

如何更改标题颜色?

Mar*_*sel 65

外观api继续发展,UITextAttributeTextColor现在替换为NSForegroundColorAttributeName.

[navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
Run Code Online (Sandbox Code Playgroud)

我将添加两件事:
首先是密钥,然后是对象.

如果要全局更改导航控制器的标题属性,请使用外观api:

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
Run Code Online (Sandbox Code Playgroud)

我将这个外观api调用放在你的app delegate的didFinishLaunchingWithOptions方法中.

更新:不妨发布Swift等价物.

要更新单个视图控制器的navigationBar,可以使用:

self.navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
Run Code Online (Sandbox Code Playgroud)

要在整个应用程序中更改导航栏的外观,可以使用:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
Run Code Online (Sandbox Code Playgroud)


wol*_*fan 11

不适合我.使用UINavigation作为模态视图控制器.

显然标题颜色需要设置uiapplication级别

编辑:

最好的方法是在每个VC上下文中更改导航标题:

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]];
Run Code Online (Sandbox Code Playgroud)


Liv*_*oy7 7

[navigationController.navigationBar setBarStyle:UIBarStyleBlack];
Run Code Online (Sandbox Code Playgroud)