Programmatically set image and text on UIButton Programmatically set image and text on UIButton ios ios

Programmatically set image and text on UIButton


UIButtons setImage sets the image above the title so you will not be able to see the text below it.So try to set image to your button with setBackgroundImage.

Objective-C:

[btn setBackgroundImage:buttonImage forState:UIControlStateNormal]; 

Swift:

myButton.setBackgroundImage(buttonImage, forState: .normal)


You've made a mistake there, you're doing

[btn setBackgroundImage:buttonImage forState:UIControlStateNormal]; 

instead of

[btn setImage:buttonImage forState:UIControlStateNormal]; 

This will work fine.


Have you tried

[btn setBackgroundImage:buttonImage forState:UIControlStateHighlighted];

It might solve your problem.