Reputation: 885
I have a button that displays an image from my Assets, on click of a button I want to replace the image by text,
I am doing this,
workExpExpand.setImage(nil, forState: .Normal)
workExpExpand.setTitle("Done", forState: .Normal)
the image disappears, but the text is blank.
What can I do?
Upvotes: 19
Views: 17968
Reputation: 129
This worked for me:
workExpExpand.setImage(UIImage(), forState: .Normal)
workExpExpand.setTitle("Done", forState: .Normal)
Upvotes: 3
Reputation: 15961
For swift 5 to remove image from button and set title
self.btnRecord.setImage(nil, for: .normal)
self.btnRecord.setTitle("REC", for: .normal)
Upvotes: 4
Reputation: 2417
swift 4 & 4.2
@IBOutlet weak var yourBtn:UIButton!
@IBAction func yourBtnAction(sender: UIButton) {
yourBtn.setImage(nil, forState: .Normal)
yourBtn.setTitle("Done", forState: .Normal)
}
Upvotes: 1
Reputation: 491
Here is the code when you button pressed as you get will sender of UIButton so you can change here:
IBAction func AnswerButtonA(sender: UIButton){
//sender is the button that was pressed
sender.setTitle("Done", forState: .Normal)
}
Upvotes: 0
Reputation: 433
Your code is right. But you have specify the title color as default color is white due to which you are not able to see text.
@IBAction func btnTapped(sender: AnyObject) {
btn.setImage(nil, forState: .Normal)
btn.setTitle("Done", forState: .Normal)
btn.setTitleColor(UIColor.redColor(), forState: .Normal)
}
Upvotes: 8
Reputation: 107141
Your code is correct. So I suspect you are having the same issue mentioned here. For fixing it you need to set the title color of that button, by default the title color is white and that's why you are not seeing the text:
workExpExpand.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
Upvotes: 11