ios - UIButton 突出显示的背景图像未显示

标签 ios swift uibutton

我必须使用多个按钮以编程方式创建布局,我使用以下代码创建我的按钮:

let myButton = UIButton(type: .Custom)
myButton.frame = CGRectMake(100,100,100,100)
myButton.setTitle("Normal", forState: .Normal)
myButton.setTitle("Selected", forState: [.Selected,.Highlighted])
myButton.setBackgroundImage(UIImage(named: "normal.png"), forState: .Normal)
myButton.setBackgroundImage(UIImage(named: "selected.png"), forState: [.Selected,.Highlighted])
self.view.addSubview(myButton)

标题和图像一旦按下就不会改变。

如果我引用在 Storyboard 中创建的 UIButton,则相同的代码会起作用。

我还需要设置其他属性吗?

谢谢

最佳答案

尝试将组合的 setBackgroundImage 调用分成两部分:

myButton.setBackgroundImage(UIImage(named: "selected.png"), forState: [.Highlighted])
myButton.setBackgroundImage(UIImage(named: "selected.png"), forState: [.Selected])

关于ios - UIButton 突出显示的背景图像未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36585347/

相关文章:

ios - 使用 Firebase 数据 Swift、Xcode 7 填充 UITableViewController

ios - 如何解决此 CollectionViewCell 约束问题?

ios - 从 UIViewController 中的 UITableView 删除核心数据

swift - 模棱两可的引用

ios - 滚动表格时按钮文本消失

ios - 如何在 Swift iOS 上引用回调函数?

ios - 如何动态计算具有行间距的nsattributed字符串的高度

ios - 如何使用属性上的 didSet 更改 UIView 的背景颜色?

ios - 为什么 UIButton 可以向私有(private)方法发送消息?

ios - 以编程方式更改 UIButton 文本并打开一个 url