2

我使用 XIB 创建了一个自定义导航栏和一个右导航按钮。这工作正常。但我需要自定义右侧导航按钮的色调。目前这个色调颜色与导航栏的色调颜色相同。我需要为这个右键设置不同的颜色。有什么办法可以改变这种颜色吗?

谢谢你。

4

4 回答 4

15

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

[[UIBarButtonItem appearance] setTintColor:YOUR_COLOR];

于 2012-05-01T09:34:35.077 回答
5

据我所知,该按钮继承了导航栏的色调。你可以做的是为 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:UIView或者initWithTitle:NSString你可以使用。

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

于 2010-12-23T15:21:09.590 回答
1

斯威夫特 3.x

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : Color.primaryActionColor], for: .normal)
于 2017-02-02T13:08:41.517 回答
0

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

navigationItem.rightBarButtonItem?.customView as? UIButton

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

button.setTitleColor(UIColor.black.withAlphaComponent(0.1), for: .normal)
于 2019-04-12T12:23:42.527 回答