gpt4 book ai didi

ios - 以编程方式创建按钮并设置背景图像

转载 作者:IT王子 更新时间:2023-10-29 04:57:41 24 4
gpt4 key购买 nike

当我尝试在 Swift 中创建按钮并设置背景图片时:

let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setImage(IMAGE, forState: UIControlState.Normal)
button.addTarget(self, action: "btnTouched:", forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(button)

我总是收到错误消息:“无法将表达式的类型‘Void’转换为类型‘UIImage’”。

最佳答案

你的代码应该如下所示

let image = UIImage(named: "name") as UIImage?
let button = UIButton(type: UIButtonType.Custom) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside)
self.view.addSubview(button)

关于ios - 以编程方式创建按钮并设置背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24064740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com