swift - 如何使 UIButton 具有正确的图像和文本外观?

标签 swift image text uibutton

我有一个 UIButton,其中包含图像和文本。我对它的外观很满意,除了按下它的时候。

在按下之前它看起来像这样: enter image description here

按下后看起来像这样: enter image description here

我习惯了按下时整个按钮变暗的外观,但出于某种原因,当我同时拥有图像和文本时,会将所有颜色指定给图像。我很清楚我可以把整个按钮做成一个图像,但我试图让这个项目尽可能远离那种风格。我创建的所有其他按钮都没有图像。

有什么方法可以使整个按钮像当前设置的那样变暗吗?

最佳答案

您必须将 UIButton 设置为自定义类型(无系统类型)并更改该属性:

self.yourButton.adjustsImageWhenHighlighted = false

如果我得到你想要的,请尝试使用该代码:

override func viewDidLoad() {
    super.viewDidLoad()
    self.button.backgroundColor = UIColor.redColor()
}


@IBAction func buttonClicked(sender: UIButton) { 
    //Touch Up Inside action
    sender.backgroundColor = UIColor.redColor()
}

@IBAction func buttonReleased(sender: UIButton) { 
    //Touch Down action
    sender.backgroundColor = UIColor.grayColor()

}

正常:

enter image description here

按下:

enter image description here

关于swift - 如何使 UIButton 具有正确的图像和文本外观?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41251493/

相关文章:

swift - 带有 let 的三元语法更好?

c# - 来自不同文件类型的文本提取库,PDF、DOC、DOCX、TXT c#

ios - UICollectionView 的索引路径日期

php - 使用 2 个文本字段将多个图像上传到数据库

CSS 图像悬停在 Internet Explorer 和 Firefox 上不起作用

javascript - 谷歌地图之类的图像工具

java - 动态改变文本框JTextArea的大小

actionscript-3 - ActionScript 3 : dynamic text help: scoring for game

ios - 如何在 Swift 3 中将 captureStillImageAsynchronously(sampleBuffer) 转换为 base64 编码

swift - 找出 UIKeyboard.frame 何时与其他框架相交?