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

这一生的挚爱 提交于 2019-12-05 17:06:26

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

Obj-Swift

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!