Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove image and write text in a UIButton in Swift?

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?

like image 667
Parth Tiwari Avatar asked Jan 10 '16 07:01

Parth Tiwari


3 Answers

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)
like image 188
Midhun MP Avatar answered Oct 24 '22 01:10

Midhun MP


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)

    }
like image 8
Chirag kukreja Avatar answered Oct 24 '22 01:10

Chirag kukreja


For swift 5 to remove image from button and set title

self.btnRecord.setImage(nil, for: .normal)
self.btnRecord.setTitle("REC", for: .normal)
like image 4
Hardik Thakkar Avatar answered Oct 24 '22 00:10

Hardik Thakkar