ios - 如何在点击后更改 UIButton 图像?

标签 ios swift uibutton

场景是我想在点击按钮后更改按钮的图像。我确实喜欢这样:

func setImage(name: String, button: UIButton)
{
    let image = UIImage(contentsOfFile: name) as UIImage?
    button.setImage(image, forState: .Normal)

}

@IBAction func ReadValueButtonClicked(sender: UIButton) {
    setImage("ic_operation_start_indications_normal.png", button: sender)
}

这根本不会带来任何改变。

有什么建议吗?

最佳答案

您需要使用 UIImagecontentsOfFile 初始值设定项指定完整路径。如果您的资源包中只有图像的文件名,请使用 imageNamed: 初始值设定项。

关于ios - 如何在点击后更改 UIButton 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33421580/

相关文章:

iphone - iOS - 将联系人添加到联系人中?

objective-c - MBProgressHud 和 SDWebImagePrefetcher

ios - UICollectionViewCell 外的按钮

ios - unity iOS link.xml 位置

ios - Swift iOS 构建期间的 "clang: error: cannot specify -o when generating multiple output files"

ios - AppDelegate : call function only if theres a new notification, 怎么办?

swift - 设置Widget的TimelineProvider刷新间隔

ios - 在 UITextField 中添加一个 UIButton

iphone - iOS,为带有导航栏、标签栏、搜索栏和键盘的 iphone 4+5 设置表格高度始终可见?

ios - 如何使用 Swift 跨 ViewControllers 继续一个功能?