Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set selected image of UIButton in XCode Interface Builder

I have two image sets for "flag" button in my Images.xcassets: flag-highlighted and flag-normal, where each set has 3 images of size x1, x2, x3

I use the flag-normal image set for the Image attribute in the attributes inspector for this button. But, where/how do I specify the flag-highlighted image set for the selected state?

like image 758
andrewz Avatar asked Mar 02 '16 01:03

andrewz


People also ask

How do you center an image in UIButton?

You must set the content mode for the image inside the UIButton. The solution is to update the contentMode of the Image when you are using a foreground image inside a button along with the contentMode of the UIButton .

How can I check if UIButton is pressed?

You can use the following code. class ViewController: UIViewController { var gameTimer: Timer! @IBAction func btnClick(_ sender: Any) { let button = UIButton() if (button. isSelected) { print(" Not Selected"); } else { print(" Selected"); } } override func viewDidLoad() { super.

What is UIButton?

A control that executes your custom code in response to user interactions.


2 Answers

You can set image for specific button state via interface builder by using state config option.

State config option

Choose selected option and then pick your image for selected state.

like image 136
seto nugroho Avatar answered Nov 13 '22 17:11

seto nugroho


If anyone is wondering why the state config is missing for button in xcode 13, you need to change button's style from plain to default after that state config drop down will appear and you can change it to whatever you want

enter image description here

like image 43
Gulfam Khan Avatar answered Nov 13 '22 17:11

Gulfam Khan