如何在 Swift 中更改 UIButton 图像

我试图改变图像的 UIButton 使用雨燕..。 我该怎么办

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

[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
306199 次浏览

在 Obc-C 代码中,我认为你需要为按钮设置一个 Image,所以试试这个方法:

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)

对于斯威夫特3:

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

你实际上可以通过高亮按钮来做到这一点,并且在右边工具栏的插件中你可以更新图像。显然,你可以在代码中也如前所述,但这是你的另一个选择

我更喜欢首先在顶部初始化变量的方法:

let playButton:UIButton!
var image:UIImage!

然后在 viewDidLoad 中设置它们

override func viewDidLoad {
...
playButton = UIButton(type: .Custom)
imagePlay = UIImage(named:"play.png")
playButton.setImage(imagePlay, forState: .Normal)
}

假设这是你连接的 UIButton Name

@IBOutlet var btn_refresh: UIButton!

你可以直接把你的图像在三种模式

 // for normal state
btn_refresh.setImage(UIImage(named: "xxx.png"), forState: UIControlState.Normal)
// for Highlighted state
btn_refresh.setImage(UIImage(named: "yyy.png"), forState: UIControlState.Highlighted)


// for Selected state
btn_refresh.setImage(UIImage(named: "zzzz.png"), forState: UIControlState.Selected)

按下按钮

  //MARK: button_refresh action
@IBAction func button_refresh_touchup_inside(sender: UIButton)
{
//if you set the image on same  UIButton
sender.setImage(UIImage(named: "newimage.png"), forState: UIControlState.Normal)


//if you set the image on another  UIButton
youranotherbuttonName.setImage(UIImage(named: "newimage.png"), forState: UIControlState.Normal)
}

从 Swift 3.0开始。正常状态已被删除。您可以使用以下命令应用正常状态。

myButton.setTitle("myTitle", for: [])

对于任何使用 Assets.xcassets 和 Swift 3的人来说,都是这样的(不需要. png)

let buttonDone  = UIButton(type: .Custom)


if let image = UIImage(named: "Done") {
self.buttonDone.setImage(image, for: .normal)
}

迅捷3.0 :

@IBOutlet weak var selectionButton: UIButton!


selectionButton.setImage(UIImage.addBlueIcon, for: .selected)

是的,甚至我们可以改变图像的 UIButton,通过使用标志。

class ViewController: UIViewController
{
@IBOutlet var btnImage: UIButton!
var flag = false


override func viewDidLoad()
{
super.viewDidLoad()


//setting default image for button
setButtonImage()


}


@IBAction func btnClick(_ sender: Any)
{
flag = !flag
setButtonImage()
}


func setButtonImage(){
let imgName = flag ? "share" : "image"
let image1 = UIImage(named: "\(imgName).png")!
self.btnImage.setImage(image1, for: .normal)
}


}

在这里,每次点击后,您的按钮图像将变化。

在 Swift 4(Xcode 9)中 将按钮图片转换为开或关(btnRec)的示例:

var bRec:Bool = true


@IBOutlet weak var btnRec: UIButton!
@IBAction func btnRec(_ sender: Any) {
bRec = !bRec
if bRec {
btnRec.setImage(UIImage(named: "MicOn.png"), for: .normal)
} else {
btnRec.setImage(UIImage(named: "MicOff.png"), for: .normal)
}
}

在 Swift 4.2和 Xcode 10.1中

选择 UIButton添加图像

button.addTarget(self, action: #selector(self.onclickDateCheckMark), for: .touchUpInside)//Target
button.setImage(UIImage.init(named: "uncheck"), for: UIControl.State.normal)//When selected
button.setImage(UIImage.init(named: "check"), for: UIControl.State.highlighted)//When highlighted
button.setImage(UIImage.init(named: "check"), for: UIControl.State.selected)//When selected

但是,如果你想 改变选定的按钮图像你需要改变它的选择状态。然后只有选定的图像将出现在您的按钮。

@objc func onclickDateCheckMark(sender:UIButton) {
if sender.isSelected == true {
sender.isSelected = false
print("not Selected")
}else {
sender.isSelected = true
print("Selected")


}
}

5,并确保图像缩放到按钮的大小,但停留在按钮的边界。

yourButton.clipsToBounds = true
yourButton.contentMode = .scaleAspectFill


// Use setBackgroundImage or setImage
yourButton.setBackgroundImage(UIImage(named: "yourImage"), for: .normal)

Swift 5

yourButton.setImage(UIImage(named: "BUTTON_FILENAME.png"), for: .normal)