gpt4 book ai didi

ios - 如何在 Swift 中更改 UIButton 图像

转载 作者:IT王子 更新时间:2023-10-29 04:56:26 25 4
gpt4 key购买 nike

我正在尝试使用 Swift 更改 UIButton 的图像...我该怎么办

这是 OBJ-C 代码。但我不知道 Swift:

[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

最佳答案

根据你的 Obc-C 代码,我认为你想为按钮设置一个图像,所以试试这个方法:

let playButton  = UIButton(type: .Custom)
if let image = UIImage(named: "play.png") {
playButton.setImage(image, forState: .Normal)
}

简而言之:

playButton.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)

对于 swift 3:

let playButton  = UIButton(type: .custom)
playButton.setImage(UIImage(named: "play.png"), for: .normal)

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

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