问题
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.
回答1:
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:
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...
回答2:
Why don't you do that programatically? It will be much simpler.
[cancelButton setBackgroundImage:cancelImg forState:UIControlStateNormal];
[cancelButton setBackgroundImage:selectedImg forState:UIControlStateSelected];
回答3:
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):
- for each State Config
- 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.
来源:https://stackoverflow.com/questions/16108754/xcode-set-image-on-button-only-for-default-state-not-also-selected