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

时间:2022-11-20 23:05:44

I've got a UINavigationController and i've changed it to white using the Tint property of the navigation bar in Interface Builder. But the text in buttons and the title is still the default color, white, and so gets lost against the white background. Anyone know how to work around this?

我有一个UINavigationController,我用界面构建器中导航栏的Tint属性把它改成白色。但是按钮和标题中的文本仍然是默认的颜色,白色,因此在白色背景下会丢失。有人知道怎么解决这个问题吗?

8 个解决方案

#1


31  

As of iOS 5 this it much easier, using the UIBarButtonItem appearance proxy:

在ios5中,使用UIBarButtonItem外观代理更容易:

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

[[UIBarButtonItem appearance] setTitleTextAttributes: attributes
                                            forState: UIControlStateNormal];

Set this in application:didFinishLaunchingWithOptions:

设置此应用程序:didFinishLaunchingWithOptions:

#2


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];
    }

}

#3


8  

Here's one way:

这里有一个方法:

[[theNavigationBar.subviews objectAtIndex:1] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[[theNavigationBar.subviews objectAtIndex:2] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

However, HUGE, caveat. This is highly likely to break on a future OS release and is not recommended.

然而,巨大的警告。这很可能会在未来的OS版本中中断,不建议这样做。

At the very least you should perform a lot of testing and make sure you your assumptions of the subview layout of the navigation bar are correct.

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

#4


2  

Or you use your own button bar item subclass with a setter you specify, lus isn't iPhone os 3 suppose to exposé text color for EVERY button

或者您使用自己的按钮栏项目子类,使用您指定的setter, lus不是iPhone os 3,假设为每个按钮公开文本颜色。

#5


2  

I did as drunknbass suggested. I resorted to making a series of images for back-button in a few states, and regular buttons in a few states, and put them in a custom UIButton subclass, setting up the appropriate styles.

我听从了酒鬼巴斯的建议。我在一些状态中为back-button创建了一系列图像,在一些状态中创建了常规按钮,并将它们放在自定义的UIButton子类中,设置了适当的样式。

As a solution it doesn't scale particularly well, but my needs were simple enough. It has the advantage of not breaking if the subview orders in the built in controls change though, which is the obvious downside of that approach. Disadvantage of needing several images. The other tricky thing is handling an orientation change, since the buttons should change size.

作为一种解决方案,它不能很好地扩展,但我的需求足够简单。如果内建控件中的子视图命令发生了更改,那么它的优点是不会中断,这是这种方法的明显缺点。需要几个图像的缺点。另一件棘手的事情是处理方向改变,因为按钮应该改变大小。

On a related note, if you do change the tint color of your navigation bar, it does not play well with the special "More" view controller for customizing the order of a tab bar. There doesn't seem to be any "acceptable" way to change the color of the toolbar in that view.

与此相关的是,如果您确实更改了导航栏的颜色,那么它就不能很好地使用专门的“More”视图控制器来定制选项卡栏的顺序。似乎没有任何“可接受的”方法可以改变该视图中工具栏的颜色。

#6


1  

Can't you iterate the contents of the UINavigationBar view to find the buttons in the awakeFromNib?

难道你不能迭代UINavigationBar视图的内容来找到awakeFromNib中的按钮吗?

#7


0  

Here the clean way (relying on public API) to do this IMHO : you should use the titleView property of the navigationItem to apply it your own UILabel on which you will customize the textColor

在这里,使用干净的方式(依赖于公共API)来实现这个IMHO:您应该使用navigationItem的titleView属性来应用它自己的UILabel,您将在其上定制textColor


UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
lbl.textAlignment=UITextAlignmentCenter;
lbl.backgroundColor=[UIColor clearColor];
lbl.textColor=[UIColor colorWithRed:0.18 green:0.2 blue:0.56 alpha:1];
theControllerOnWhichYouWantToHaveATitleWithYourOwnColor.navigationItem.titleView=lbl;
[lbl release];

#8


-1  

you can change title appearance by adding label into the navigation bar like bellow.

您可以通过将标签添加到像bellow这样的导航栏中来更改标题外观。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 35)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;
self.navigationItem.titleView = viewLabel;
[label release]; 

#1


31  

As of iOS 5 this it much easier, using the UIBarButtonItem appearance proxy:

在ios5中,使用UIBarButtonItem外观代理更容易:

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

[[UIBarButtonItem appearance] setTitleTextAttributes: attributes
                                            forState: UIControlStateNormal];

Set this in application:didFinishLaunchingWithOptions:

设置此应用程序:didFinishLaunchingWithOptions:

#2


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];
    }

}

#3


8  

Here's one way:

这里有一个方法:

[[theNavigationBar.subviews objectAtIndex:1] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[[theNavigationBar.subviews objectAtIndex:2] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

However, HUGE, caveat. This is highly likely to break on a future OS release and is not recommended.

然而,巨大的警告。这很可能会在未来的OS版本中中断,不建议这样做。

At the very least you should perform a lot of testing and make sure you your assumptions of the subview layout of the navigation bar are correct.

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

#4


2  

Or you use your own button bar item subclass with a setter you specify, lus isn't iPhone os 3 suppose to exposé text color for EVERY button

或者您使用自己的按钮栏项目子类,使用您指定的setter, lus不是iPhone os 3,假设为每个按钮公开文本颜色。

#5


2  

I did as drunknbass suggested. I resorted to making a series of images for back-button in a few states, and regular buttons in a few states, and put them in a custom UIButton subclass, setting up the appropriate styles.

我听从了酒鬼巴斯的建议。我在一些状态中为back-button创建了一系列图像,在一些状态中创建了常规按钮,并将它们放在自定义的UIButton子类中,设置了适当的样式。

As a solution it doesn't scale particularly well, but my needs were simple enough. It has the advantage of not breaking if the subview orders in the built in controls change though, which is the obvious downside of that approach. Disadvantage of needing several images. The other tricky thing is handling an orientation change, since the buttons should change size.

作为一种解决方案,它不能很好地扩展,但我的需求足够简单。如果内建控件中的子视图命令发生了更改,那么它的优点是不会中断,这是这种方法的明显缺点。需要几个图像的缺点。另一件棘手的事情是处理方向改变,因为按钮应该改变大小。

On a related note, if you do change the tint color of your navigation bar, it does not play well with the special "More" view controller for customizing the order of a tab bar. There doesn't seem to be any "acceptable" way to change the color of the toolbar in that view.

与此相关的是,如果您确实更改了导航栏的颜色,那么它就不能很好地使用专门的“More”视图控制器来定制选项卡栏的顺序。似乎没有任何“可接受的”方法可以改变该视图中工具栏的颜色。

#6


1  

Can't you iterate the contents of the UINavigationBar view to find the buttons in the awakeFromNib?

难道你不能迭代UINavigationBar视图的内容来找到awakeFromNib中的按钮吗?

#7


0  

Here the clean way (relying on public API) to do this IMHO : you should use the titleView property of the navigationItem to apply it your own UILabel on which you will customize the textColor

在这里,使用干净的方式(依赖于公共API)来实现这个IMHO:您应该使用navigationItem的titleView属性来应用它自己的UILabel,您将在其上定制textColor


UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
lbl.textAlignment=UITextAlignmentCenter;
lbl.backgroundColor=[UIColor clearColor];
lbl.textColor=[UIColor colorWithRed:0.18 green:0.2 blue:0.56 alpha:1];
theControllerOnWhichYouWantToHaveATitleWithYourOwnColor.navigationItem.titleView=lbl;
[lbl release];

#8


-1  

you can change title appearance by adding label into the navigation bar like bellow.

您可以通过将标签添加到像bellow这样的导航栏中来更改标题外观。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 35)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;
self.navigationItem.titleView = viewLabel;
[label release];