我正在尝试使用Swift更改UIButton的图像... 我该怎么办
这是OBJ-C代码。但我不知道Swift:
[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
答案 0 :(得分:279)
从你的Obc-C代码我想你想设置一个Image for按钮,所以试试这个:
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)
答案 1 :(得分:9)
假设这是您的联系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)
}
答案 2 :(得分:7)
对于使用Assets.xcassets和Swift 3的任何人来说,它都是这样的(不需要.png)
let buttonDone = UIButton(type: .Custom)
if let image = UIImage(named: "Done") {
self.buttonDone.setImage(image, for: .normal)
}
答案 3 :(得分:7)
在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)
}
}
答案 4 :(得分:6)
从swift 3.0开始,已经删除了正常状态。您可以使用以下来应用正常状态。
myButton.setTitle("myTitle", for: [])
答案 5 :(得分:5)
我更喜欢先在顶部初始化变量的方法:
let playButton:UIButton!
var image:UIImage!
然后在viewDidLoad中设置它们
override func viewDidLoad {
...
playButton = UIButton(type: .Custom)
imagePlay = UIImage(named:"play.png")
playButton.setImage(imagePlay, forState: .Normal)
}
答案 6 :(得分:3)
是的,即使我们可以使用标志来更改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)
}
}
在这里,每次点击后,您的按钮图像都会改变。
答案 7 :(得分:3)
迅速移动5并确保图像缩放到按钮的大小,但保持在按钮范围之内。
yourButton.clipsToBounds = true
yourButton.contentMode = .scaleAspectFill
// Use setBackgroundImage or setImage
yourButton.setBackgroundImage(UIImage(named: "yourImage"), for: .normal)
答案 8 :(得分:3)
快捷键5
yourButton.setImage(UIImage(named: "BUTTON_FILENAME.png"), for: .normal)
答案 9 :(得分:1)
您可以通过突出显示按钮实际执行此操作,并在右侧工具栏的insepctor中更新图像。显然你也可以像前面所说的那样在代码中完成它,但这是你的另一个选择
答案 10 :(得分:0)
:
@IBOutlet weak var selectionButton: UIButton!
selectionButton.setImage(UIImage.addBlueIcon, for: .selected)
答案 11 :(得分:0)
在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")
}
}