Wiingaard
Wiingaard

Reputation: 4302

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

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.

Upvotes: 4

Views: 13818

Answers (3)

lakshmen
lakshmen

Reputation: 29064

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...

Upvotes: 8

bago
bago

Reputation: 108

If you want to do it programmatically the answer of akash is the way to go. But if you want to do so from interface builder you should follow these 2 steps (image here: https://www.evernote.com/shard/s29/sh/9abb1987-614d-4326-b5bb-bcff28756ee4/85d6ca236276166992ff01a082222e96):

  1. for each State Config
  2. select the image you want or left it empty

Xcode put the same image you add for the Default state in the Selected state but if you change the second it will not be overwritten automatically.

Upvotes: 0

Obj-Swift
Obj-Swift

Reputation: 2942

Why don't you do that programatically? It will be much simpler.

[cancelButton setBackgroundImage:cancelImg forState:UIControlStateNormal];
[cancelButton setBackgroundImage:selectedImg forState:UIControlStateSelected];

Upvotes: 0

Related Questions