xcode 导航栏按钮项目色调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22790612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:49:34  来源:igfitidea点击:

Navigation Bar Button Item Tint

iosxcodeuibarbuttonitem

提问by I'm Joe Too

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?

示例:我的深色背景导航栏的默认色调为白色,但我想为其中一个按钮显示一个橙色图像图标。如果我拖动一个条形按钮项目并设置图像,它会显示为白色。我可以更改(或删除)这个按钮的色调吗?或者,我是否需要忘记可视化编辑器并以编程方式创建此按钮来控制色调?

回答by lootsch

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 (e.g.: orange only) you can achieve that inside your storyboard. Just set the tintcolor of your Bar Button Iteminside Xcode's Attributes Inspectorto the desired color:
Xcode Screenshot

如果您想覆盖按钮的色调(例如:仅限橙色),您可以在故事板中实现。只需将内部 Xcode 的tint颜色设置为所需的颜色:Bar Button ItemAttributes Inspector
Xcode 截图

回答by brookinc

Here's lootsch's (excellent) answer converted to Swift 3 / Swift 4:

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

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

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

回答by mgm

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)
  2. Assign the image to the button from code:

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

  1. 在您的类文件(通常是 ViewController)中引用您的 Button Outlet
  2. 将图像从代码分配给按钮:

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