如何将 UIBarButtonItem tintcolor 设置为非默认 UIColor

Posted

技术标签:

【中文标题】如何将 UIBarButtonItem tintcolor 设置为非默认 UIColor【英文标题】:How to set UIBarButtonItem tintcolor to non default UIColor 【发布时间】:2015-03-20 22:58:19 【问题描述】:

我浏览了所有典型的导航栏 tintcolor 教程和问题。我为导航栏设置了色调,但我有一个邮件图标,需要在有邮件时更改为自定义颜色。 (如 reddit 橙色邮件图标)

只有在使用系统 UIColors 时才能正确设置色调。

self.leftNavigationBarButton = [[UIBarButtonItem alloc] initWithImage:someImage style:UIBarButtonItemStylePlain target:self action:@selector(foo:)];
self.navigationItem.leftBarButtonItem = self.leftNavigationBarButton;

self.leftNavigationBarButton.tintcolor = [UIColor redColor];

但是,如果我随后使用自定义颜色。

self.leftNavigationBarButton.tintcolor = [UIColor colorWithRed:100 green:40 blue:20 alpha:1.0];

它使图标变白。有谁知道发生了什么或如何使用自定义颜色?

【问题讨论】:

【参考方案1】:

我从https://***.com/a/5642229/1732711这个答案中发现了。

简短的回答是将 RGB 值除以 255.0。

self.leftNavigationBarButton.tintcolor = [UIColor colorWithRed:100/255.0 green:40/255.0 blue:20/255.0 alpha:1.0];

【讨论】:

以上是关于如何将 UIBarButtonItem tintcolor 设置为非默认 UIColor的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UIImageView.image 设置为 UIBarButtonItem

如何将配置文件图像放置在目标 c 中圆角的 uibarbuttonitem 上?

如何在返回箭头中更改 UIBarButtonItem?

如何将 UIBarButtonItem 放在 Swift4 上 NavigationController 的 ViewController 上?

如何在 Swift 中为 UIBarButtonItem 设置动作

如何以编程方式将 UIBarButtonItem 添加到拖到视图上的 UINavigationBar