Xcode: set image on button only for default state, not also selected

Wiingaard picture Wiingaard · Apr 19, 2013 · Viewed 13.6k times · Source

I am new to Xcode and iPhone programming in general.. I have a simple problem thats bugging me.. I want to set an image on a button, for it's default state. So I select the button, and in the "Show the attributes inspector"-tab, I select "state config" to be default, then I find the desired image in "image"-drop down list...

Problem:

I change the "state config" to selected, and the image is still on. I need the image to only be on in default state. I have same problem if i want different font size of the title text in default- and selected-state. I'm using Xcode 4.6 and writing for iOS 6.1.

Answer

lakesh picture lakesh · Apr 19, 2013

There are two ways.

First way, programmatically:

[button setBackgroundImage:buttonimg forState:UIControlStateSelected];
[button setBackgroundImage:buttonimg forState:UIControlStateNormal];
[button setBackgroundImage:buttonimg forState:UIControlStateHighlighted];

Second way, using IB Builder:

Like the img:

enter image description here

You have a option of selecting a state config. Based on the config, you can set the image under the section called "Image".

Hope this helps...