如何更改UINavigationBar按钮项目上的文本颜色

law*_*nce 19 iphone cocoa-touch

我有一个UINavigationController,我使用Interface Builder中导航栏的Tint属性将其更改为白色.但按钮和标题中的文本仍然是默认颜色,白色,因此在白色背景下丢失.任何人都知道如何解决这个问题?

Mic*_*ael 31

从iOS 5开始,使用UIBarButtonItem外观代理更加容易:

 NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
                        [UIColor blackColor], 
                        UITextAttributeTextColor, 
                        [UIColor clearColor], 
                        UITextAttributeTextShadowColor, nil];

[[UIBarButtonItem appearance] setTitleTextAttributes: attributes
                                            forState: UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

在应用程序中设置:didFinishLaunchingWithOptions:


Tar*_*pun 11

for (id subView in theNavigationBar.subviews) {
    if ([subView isKindOfClass:[UIButton class]]) {
        [(UIButton *)subView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [(UIButton *)subView setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
    }

}
Run Code Online (Sandbox Code Playgroud)


sch*_*hwa 8

这是一种方式:

[[theNavigationBar.subviews objectAtIndex:1] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[[theNavigationBar.subviews objectAtIndex:2] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

但是,巨大的,警告.这很可能在未来的操作系统版本中打破,不建议使用.

至少你应该进行大量的测试,并确保你对导航栏的子视图布局的假设是正确的.