Xcode: set image to button only for default state and also not selected

I am new to programming on Xcode and iPhone in general. I have a simple problem that pushes me. I want to set the image to a button as this is the default state. Thus, I select the button, and in the "Show Attribute Inspector" -tab, I select the "state config" by default, then I find the desired image in the "image" -drop down list ...

Problem:

I change the “state configuration” to the selected one and the image is still on. I need the image to display only in the default state. I have the same problem if I need a different font size for the default header text and the selected state. I am using Xcode 4.6 and writing for iOS 6.1.

+4
source share
3 answers

There are two ways.

The first way, programmatically:

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

The second way using IB Builder:

Like img:

enter image description here

You have the option to choose a state configuration. Based on the configuration, you can set the image in the "Image" section.

Hope this helps ...

+8
source

Why don't you do it programmatically? It will be a lot easier.

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

If you want to do this programmatically, akash's answer is the way to go. But if you want to do this from the interface constructor, you must follow these two steps (image here: https://www.evernote.com/shard/s29/sh/9abb1987-614d-4326-b5bb-bcff28756ee4/85d6ca236276166992ff01a082222e96 ):

  • for each state configuration
  • select the image you want or leave it blank.

Xcode put the same image that you add for the default state in the selected state, but if you change the second, it will not be overwritten automatically.

0
source

All Articles