Xcode:仅在默认状态下在按钮上设置图像,未选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16108754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode: set image on button only for default state, not also selected
提问by Wiingaard
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...
我是 Xcode 和 iPhone 编程的新手。我有一个简单的问题困扰着我。我想在按钮上设置一个图像,因为它是默认状态。所以我选择按钮,并在“显示属性检查器”选项卡中,选择“状态配置”作为默认值,然后在“图像”下拉列表中找到所需的图像...
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.
我将“状态配置”更改为选中,图像仍然打开。我需要图像仅在默认状态下打开。如果我想在默认状态和选定状态下标题文本的字体大小不同,我会遇到同样的问题。我正在使用 Xcode 4.6 并为 iOS 6.1 编写代码。
回答by lakesh
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:
第二种方式,使用IB Builder:
Like the img:
像 img:
You have a option of selecting a state config. Based on the config, you can set the image under the section called "Image".
您可以选择状态配置。根据配置,您可以在名为“Image”的部分下设置图像。
Hope this helps...
希望这可以帮助...
回答by Obj-Swift
Why don't you do that programatically? It will be much simpler.
你为什么不以编程方式做到这一点?会简单很多。
[cancelButton setBackgroundImage:cancelImg forState:UIControlStateNormal];
[cancelButton setBackgroundImage:selectedImg forState:UIControlStateSelected];
回答by bago
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):
如果您想以编程方式执行此操作,那么 akash 的答案就是要走的路。但是,如果您想从界面构建器中执行此操作,则应遵循以下 2 个步骤(此处的图片:https: //www.evernote.com/shard/s29/sh/9abb1987-614d-4326-b5bb-bcff28756ee4/85d6ca236276166992ff01a09622)
- for each State Config
- select the image you want or left it empty
- 对于每个状态配置
- 选择您想要的图像或将其留空
Xcode put the same image you add for the Defaultstate in the Selectedstate but if you change the second it will not be overwritten automatically.
Xcode 将您为Default状态添加的相同图像放入Selected状态,但如果您更改第二个图像,它不会自动覆盖。