How to change button image in swift?

拈花ヽ惹草 提交于 2020-01-12 06:03:10

问题


I am working on an app which has a button. The button has no text, image or background.

So what I want to do is to give it an image in the viewDidLoad function.

This is what I have:

@IBOutlet var tapButton: UIButton!

override func viewDidLoad() {

    super.viewDidLoad()

    tapButton.setImage("redTap.png", forState: UIControlState.Normal)
}

But I have an error that says I can not convert string to UIIMage.

How do I get it to work?

I have tried:

let image = UIImage(named: "redTap.png")

tapButton.setImage(image, forState: UIControlState.Normal)

I have gotten it to work but now I have a problem.

The image is suppose to be a red text but it shows up blue.

I was able to get the image to show correctly by using:

let image = UIImage(named: imageColor[randNum])?.imageWithRenderingMode(.AlwaysOriginal)
tapButton.setImage(image, forState: UIControlState.Normal)

What I want now is to not have the button be highlighted when the button is pressed. I have a few other buttons that have images assigned to them in xcode and not through code. They don't highlight when pressed.

So how can I get rid of highlighting when the button is pressed?


回答1:


You don't need ".png".

If ".imageWithRenderingMode(.AlwaysOriginal)" is working for you: To keep the same image in different states, you have to set the same image/properties for the different states.

@IBOutlet var tapButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    tapButton.setImage(UIImage(named: "redTap")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
    tapButton.setImage(UIImage(named: "redTap")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Highlighted)
}



回答2:


Swift 3

yourBtn.setImage( UIImage.init(named: "imagename"), for: .normal)



回答3:


now (swift 3 edition):

@IBOutlet var tapButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    tapButton.setImage(UIImage(named: "redTap")?.withRenderingMode(.alwaysOriginal), for: .normal)
    tapButton.setImage(UIImage(named: "redTap")?.withRenderingMode(.alwaysOriginal), for: .normal)
}



回答4:


Let image = UIImage(named : "redTap.png") tapButton.setImage(image, .Normal)




回答5:


First I would put the image you wanted inside the Assets.xcassets folder. Just drag it in. Then you can call it whatever you want by double-clicking on it. Lets say that it is called "redTap".

For code you would put:

@IBOutlet var tapButton: UIButton!

override func viewDidLoad() {

    super.viewDidLoad()

    let redTapImage = UIImage(named: "redTap")
    tapButton.setImage(redTapImage, forState: UIControlState.Normal)
}



回答6:


let button = UIButton(type: .Custom)
let image = UIImage(named:"redTap")?.imageWithRenderingMode(.AlwaysTemplate)
button.setImage(image, forState: .Normal)
button.tintColor = UIColor.redColor()



回答7:


There's one simple solution:

@IBOutlet var tapButton: UIButton!{
didSet{
let image = UIImage(named: imageColor[randNum])
tapButton.setImage(image, forState: UIControlState.Normal)
}

Besides that, in the Attribute Inspector, Set Button type to 'Custom'




回答8:


First set your button type as Custom in interface builder and set the image for normal state for your button.

Connect IBOutlet for button in class file as

@IBOutlet weak var btnCheckbox: UIButton!

in your class file in viewDidLoad set image for selected state as

btnCheckbox.setImage(UIImage.init(named: "name_of_image"), for: .selected)

Create @IBAction for in class file as

@IBAction func onTapCheckBox(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
}

Then connect @IBAction to your button's Touch up Inside event from interface builder



来源:https://stackoverflow.com/questions/38041688/how-to-change-button-image-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!