如何关闭与 UIButton 一起使用的图像上的蓝色叠加层? [英] How do I turn off the blue overlay on the image I'm using with my UIButton?

查看:19
本文介绍了如何关闭与 UIButton 一起使用的图像上的蓝色叠加层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我是一个相对较新的编码员,并且明白这可能是一个真正简单的修复,但我似乎无法在任何地方找到方法,只是人们知道它发生了.也有可能我误解了其他一些核心思想,这就是我似乎无法解决的原因.

So, I'm a relatively new coder and understand this may be a real simple fix but I can't seem to find how anywhere, just that people know it happens. It is also possible I've misunderstood some other core idea and that is why I can't seem to fix.

我在我的故事板中设置了一个 UIKit 按钮,在我的脚本中为这个按钮添加了一个 IBAction 和 IBOutlet(类?无论在项目中调用单个 .swift 文件),将 IBO 设置为从 UIButton 继承,都在 ViewController 类中.在 viewDidLoad 函数中,我有

I've set up a UIKit button in my storyboard, added both an IBAction and IBOutlet for this button in my script (class? whatever the individual .swift files are called within a project), set the IBO to inherit from UIButton, all in the ViewController class. In the viewDidLoad function I have

button.setImage(UIImage (named: "logo"), for: .normal)

和另一行与 UILabel 有关的编码器(那里一切正常).在 viewDidLoad 函数下方,我有一个数组,然后是带有一些代码的 IBA,该代码可以对数组进行混洗并将 0 索引点输出到屏幕上.

and another line of coder pertaining to a UILabel (everything is fine there). Below the viewDidLoad function I have an array and then the IBA with a little bit of code that shuffles the array and spits out 0 indexed spot onto the screen.

我已经尝试解决这个问题几天了,假设我已经理解了我读过的所有内容,我已经了解到 UIImages 是不可变的(无法更改),所以这告诉我我需要在 viewDidLoad 中声明时修复此问题.我还了解到在当前的 swift/xcode 状态下它是蓝色是默认的,并且这是一个渲染模式问题,只需将其关闭即可解决问题.我一生都无法将最后几个点联系起来,并弄清楚如何实际告诉 Xcode 只使用我提供的图像.

I've been trying to fix this for a few days now and assuming I've understood everything I've read, I've learned that UIImages are immutable (can't be changed) so that tells me that I need to fix this when declaring it in viewDidLoad. I've also learned that it being blue is default in the current swift/xcode state, and that it is a render mode issue, just turning that off will fix the problem. I can't for the life of me connect the last few dots and figure out how to actually tell Xcode to simply use the image I've provided it.

当然,我正在寻找代码解决方案,但了解它为什么会起作用以及对我认为我学到的知识的任何更正/澄清将更加感激,因此我可以在未来更多地帮助自己.

I'm looking for a code solution of course, but the knowledge of why it will work and any corrections/clarifications to what I think I've learned will be even more appreciated so I can help myself more in the future.

先谢谢你.-Cyre

推荐答案

由于您还没有指定,我假设故事板中按钮的类型"设置为系统"(拖动时的默认按钮类型在故事板中创建一个新的 UIButton).

Since you haven't specified, I'm assuming the button's "Type" is set to "System" in the storyboard (the default button type when you drag out a new UIButton in storyboards).

默认情况下,系统按钮采用设置在其上的任何图像并对其应用模板渲染模式,这使得图像将所有非透明像素渲染为平面颜色(例如,在这种情况下按钮的色调,默认情况下是您看到的系统蓝色).

By default, system buttons take any image set on it and apply a template rendering mode to it, which makes the image render all non-transparent pixels as a flat color (e.g. the tint color of the button in this case, which is by default the system blue color that you're seeing).

UIImages 默认有一个 renderingMode automatic,这意味着图像采用的实际有效渲染模式取决于使用图像的任何内容(在本例中为 UIButton).

但是,您可以使用 alwaysOriginal 以便它永远不会被视为模板图像.有几种方法可以做到这一点.

You can, however, instantiate a UIImage with a renderingMode of alwaysOriginal so that it never is treated as a template image. There are a couple of ways to do this.

您可以通过获取您已经创建的图像,在代码中创建一个具有新渲染模式的新图像对象:

You can create a new image object with a new rendering mode in code by taking the image you're already creating:

UIImage(named: "logo")

并调用 withRenderingMode(_:) 返回给您一个新的 UIImage 对象,然后您在按钮上设置该对象:

and calling withRenderingMode(_:) on it to return to you a new UIImage object that you then set on your button:

let logoImage = UIImage(named: "logo")
let logoImageAlwaysOriginal = logoImage?.withRenderingMode(.alwaysOriginal)
button.setImage(logoImageAlwaysOriginal, for: .normal)

2.资产目录

比上面的程序化路线更简单的方法是在资产目录中更改图像的默认渲染类型.在资产目录中单击要更改的图像(在本例中为徽标"),然后转到属性检查器(键盘快捷键:option+command+4),并将渲染为"属性更改为原始图像"".现在,无论何时您使用或从资产目录中获取此图像(在您的故事板或代码中),该图像都将具有 .alwaysOriginal 的渲染模式.

您可以执行上述任何选项并保留系统按钮类型,或者您可以在故事板中将按钮的类型更改为自定义",因为自定义按钮应用模板渲染模式默认为他们的图像.但是,当不使用系统按钮类型时,您会失去一些不错的东西,例如文本颜色不再取自按钮的色调,并且按钮在未按下时没有漂亮的淡入淡出动画.

You can either do any of the above options and keep the system button type, OR you can just change your button's type to "Custom" in the storyboard, as custom buttons do not apply a template rendering mode to their images by default. However, you lose some nice things when not using the system button type, like the text color no longer is taken from the button's tint color, and the button doesn't have a nice fade animation when unpressed.

这篇关于如何关闭与 UIButton 一起使用的图像上的蓝色叠加层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆