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

发布于 2024-10-08 22:02:34 字数 123 浏览 0 评论 0原文

我使用 XIB 创建了一个自定义导航栏和一个右侧导航按钮。这很好用。但我需要自定义右侧导航按钮的色调颜色。目前,该色调与导航栏的色调颜色相同。我需要为这个右侧按钮使用不同的颜色。有什么办法可以改变这个颜色吗?

谢谢。

I created a custom navigation bar and a right navigation button using the XIB. This works fine. But I need to to customize the tint color of the right navigation button. At the moment this tint color is the same color as tint color of navigation bar. I need a different color for this right button. Is there any way to change this color?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

謌踐踏愛綪 2024-10-15 22:02:34

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

[[UIBarButtonItem Appearance] setTintColor:YOUR_COLOR];

In iOS 5 there is 'appearance' feature:

[[UIBarButtonItem appearance] setTintColor:YOUR_COLOR];

无名指的心愿 2024-10-15 22:02:34

据我所知,该按钮继承了导航栏的色调颜色。您可以做的是为导航项设置一个自定义视图:

这是您如何使用 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

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

Not to my knowledge, the button inherits the tint color of the navigationBar. What you can do is set a customView for the navigationItem:

This is how you set it with one of the SDK's buttons:

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

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

Instead you can do it like this:

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

To use an image you made in photoshop etc.

There is als an initWithCustomView:UIView or initWithTitle:NSString you can use.

Sorry no "one-line solution" :)

菊凝晚露 2024-10-15 22:02:34

斯威夫特3.x

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

Swift 3.x

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : Color.primaryActionColor], for: .normal)
吻风 2024-10-15 22:02:34

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

navigationItem.rightBarButtonItem?.customView as? UIButton

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

button.setTitleColor(UIColor.black.withAlphaComponent(0.1), for: .normal)

If you have navigation bar button item with custom view you should convert to UIButton first:

navigationItem.rightBarButtonItem?.customView as? UIButton

Then you can add the attributes of UIButton such as:

button.setTitleColor(UIColor.black.withAlphaComponent(0.1), for: .normal)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文