使用Xcode 9.2,以上解决方案均无法满足我的需求。
我一直在寻找一种解决方案,可以让我在情节提要中为原始设置模式设置图像.normal和.selected UIControlState图像,但是在Swift文件中,不应该存在有关图像名称的字符串文字。
基本上,在您的代码内,您将获得在情节提要板中设置的.normal状态图像,并将其重新渲染为.alwaysOriginal(与.selected状态相同),然后,您将设置该图像(现在呈现为原始图像,并且不会受到有关您的状态(.normal和.selected)的色调)UIButton。
这里是:
// Get your .normal image (you set via your storyboard) and render it as original
let unselectedImage = yourButton.image(for: .normal)?.withRenderingMode(.alwaysOriginal)
// Set your normal image but this time rendered as original
yourButton.setImage(unselectedImage, for: .normal)
// Same for selected state
let selectedImage = yourButton.image(for: .selected)?.withRenderingMode(.alwaysOriginal)
yourButton.setImage(selectedImage, for: .selected)
这样,您可以设置按钮的图像状态,并且如果图像名称更改,则不会影响您的代码。