简体   繁体   English

导航栏按钮项目色调

[英]Navigation Bar Button Item Tint

Is it possible to drag a navigation bar button item to the navigation bar, use an image, and NOT have tint applied so that the original image appears as intended?是否可以将导航栏按钮项拖到导航栏上,使用图像,并且不应用色调以便原始图像按预期显示? Or is this only possible by adding nav bar button items via code?或者这只能通过代码添加导航栏按钮项目来实现?

Example: My dark background nav bar has white as the default tint, but I have an orange image icon I want to appear for one of the buttons.示例:我的深色背景导航栏的默认色调为白色,但我想为其中一个按钮显示一个橙色图像图标。 If I drag a bar button item & set the image, it appears white.如果我拖动一个条形按钮项目并设置图像,它会显示为白色。 Can I change (or remove) the tint for this one button?我可以更改(或删除)这个按钮的色调吗? Or, do I need to forget the visual editor and create this button programmatically to control the tint?或者,我是否需要忘记可视化编辑器并以编程方式创建此按钮来控制色调?

If you want to keep the original colors of the image, you have to do it in code: 如果要保留图像的原始颜色,则必须在代码中执行此操作:

[button setImage:[[UIImage imageNamed:@"imageName.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

If you want to overwrite the tint color of your button (eg: orange only) you can achieve that inside your storyboard. 如果要覆盖按钮的色调(例如:仅橙色),则可以在故事板中实现。 Just set the tint color of your Bar Button Item inside Xcode's Attributes Inspector to the desired color: 只需将Xcode的Attributes Inspector中的Bar Button Itemtint颜色设置为所需的颜色:
Xcode截图

这是lootsch的(优秀)答案转换为Swift 3 / Swift 4:

barButton.image = UIImage(named: "imageName")?.withRenderingMode(.alwaysOriginal)

To keep the original colours of the image, you would have to assign the image to the button from code rather than from your storyboard. 要保留图像的原始颜色,您必须从代码而不是故事板中将图像分配给按钮。

  1. Reference your Button Outlet in your class file (usually a ViewController) 在类文件中引用Button Outlet(通常是ViewController)
  2. Assign the image to the button from code: 从代码中将图像分配给按钮:

    [self.bbiHome setImage:[[UIImage imageNamed:@"icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]; [self.bbiHome setImage:[[UIImage imageNamed:@“icon.png”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

You can set the tint color by default.您可以默认设置色调颜色。

示例图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM