在自定义导航栏中设置条形按钮项目颜色

时间:2010-12-23 12:10:29

标签: iphone cocoa-touch ios4 uinavigationbar uinavigationitem

我使用XIB创建了自定义导航栏和右侧导航按钮。这很好用。但我需要自定义右侧导航按钮的色调颜色。此时此色调颜色与导航栏的色调颜色相同。这个右键我需要不同的颜色。有没有办法改变这种颜色?

谢谢。

4 个答案:

答案 0 :(得分:15)

在iOS 5中有“外观”功能:

[[UIBarButtonItem appearance] setTintColor:YOUR_COLOR];

答案 1 :(得分:5)

据我所知,该按钮继承了navigationBar的色调颜色。您可以做的是为navigationItem设置customView:

这是您使用SDK的一个按钮设置它的方式:

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButtonHandler:)];

[self.navigationItem setRightBarButtonItem:shareButton];
[shareButton release];

相反,你可以这样做:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yourImage.png" style:UIBarButtonItemStyleBordered target:self action:@selector(customButtonHandler:)]];

使用你在photoshop等制作的图像

您可以使用initWithCustomView:UIViewinitWithTitle:NSString

抱歉没有“一线解决方案”:)

答案 2 :(得分:1)

Swift 3.x

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : Color.primaryActionColor], for: .normal)

答案 3 :(得分:0)

如果您的导航栏按钮项具有自定义视图,则应先转换为 UIButton

navigationItem.rightBarButtonItem?.customView as? UIButton

然后,您可以添加 UIButton 的属性,例如:

button.setTitleColor(UIColor.black.withAlphaComponent(0.1), for: .normal)
相关问题