ios 如何在 Swift 中更改 UIButton 图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26837371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change UIButton image in Swift
提问by Lop Hu
I am trying to change the image of a UIButton using Swift... What should I do
我正在尝试使用 Swift 更改 UIButton 的图像......我该怎么办
This is OBJ-C code.but I don't know with Swift:
这是 OBJ-C 代码。但我不知道 Swift:
[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
回答by Dharmesh Kheni
From your Obc-C code I think you want to set an Image for button so try this way:
从你的 Obc-C 代码中,我认为你想为按钮设置一个图像,所以试试这种方式:
let playButton = UIButton(type: .Custom)
if let image = UIImage(named: "play.png") {
playButton.setImage(image, forState: .Normal)
}
In Short:
简而言之:
playButton.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)
For Swift 3:
对于 Swift 3:
let playButton = UIButton(type: .custom)
playButton.setImage(UIImage(named: "play.png"), for: .normal)
回答by L.N.Vu
in Swift 4, (Xcode 9) example to turn picture of button to On or Off (btnRec):
在 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)
}
}
回答by Anbu.Karthik
assume that this is your connected UIButton Name
like
假设这是您的连接UIButton Name
方式
@IBOutlet var btn_refresh: UIButton!
your can directly place your image in three modes
您可以在三种模式下直接放置图像
// 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)
on your button action
在您的按钮操作上
//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)
}
回答by DS.
For anyone using Assets.xcassets and Swift 3, it'd be like this (no need for .png)
对于任何使用 Assets.xcassets 和 Swift 3 的人来说,就是这样(不需要 .png)
let buttonDone = UIButton(type: .Custom)
if let image = UIImage(named: "Done") {
self.buttonDone.setImage(image, for: .normal)
}
回答by PatientC
As of swift 3.0 .normal state has been removed.you can use following to apply normal state.
从 swift 3.0 开始,.normal 状态已被删除。您可以使用以下内容来应用正常状态。
myButton.setTitle("myTitle", for: [])
回答by Kristian
I prefer the method of initializing my variables at the top first:
我更喜欢首先在顶部初始化变量的方法:
let playButton:UIButton!
var image:UIImage!
and then setting them in viewDidLoad
然后在 viewDidLoad 中设置它们
override func viewDidLoad {
...
playButton = UIButton(type: .Custom)
imagePlay = UIImage(named:"play.png")
playButton.setImage(imagePlay, forState: .Normal)
}
回答by Kristian
Yes, even we can change image of UIButton, by using flag.
是的,即使我们可以通过使用标志来更改 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)
}
}
Here, after every click your button image will change alternatively.
在这里,每次单击后,您的按钮图像都会交替更改。
回答by iOS
In Swift 4.2 and Xcode 10.1
在 Swift 4.2 和 Xcode 10.1 中
Add image for selected UIButton
为选定的 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
But if you want to change selected button imageyou need to change it's selected state. Then only selected image will appear in your button.
但是如果你想改变选中的按钮图像,你需要改变它的选中状态。然后只有选定的图像会出现在您的按钮中。
@objc func onclickDateCheckMark(sender:UIButton) {
if sender.isSelected == true {
sender.isSelected = false
print("not Selected")
}else {
sender.isSelected = true
print("Selected")
}
}
回答by Richy Garrincha
you can actually do this by highlighting the button and within the insepctor on the right hand tool bar you can update the image. obviously you can do it in code also as stated previously but this is another option for you
实际上,您可以通过突出显示按钮来执行此操作,并且在右侧工具栏上的检查器中您可以更新图像。显然,您也可以如前所述在代码中执行此操作,但这是您的另一种选择
回答by Marcelo Gracietti
in swift 3.0:
在 swift 3.0 中:
@IBOutlet weak var selectionButton: UIButton!
selectionButton.setImage(UIImage.addBlueIcon, for: .selected)