ios 如何设置按钮的背景颜色 - xcode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41074243/
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
How set background color of a button - xcode
提问by GMX
How can I set the background color of a UIButton
in Xcode?
如何UIButton
在 Xcode 中设置 a 的背景颜色?
I see the property Background and Image in the layout property but i can't set anything in this two. (I can set only image in both not colors)
我在布局属性中看到属性背景和图像,但我无法在这两个中设置任何内容。(我只能设置两种颜色的图像,而不是颜色)
回答by Nirav D
Just scroll a little bit in Attribute Inspector
, you can find Background
property in view
section that will allow you set the backgroundColor
for the UIButton
.
只是在滚动一点点Attribute Inspector
,你可以找到Background
在性能view
部分,可以让你设置backgroundColor
的UIButton
。
If you want to set backgroundColor
of button programatically.
如果您想以backgroundColor
编程方式设置按钮。
Objective C :
目标C:
self.yourButton.backgroundColor = [UIColor redColor];
Swift 3 and Swift 4
斯威夫特 3 和斯威夫特 4
self.yourButton.backgroundColor = UIColor.red
Swift 2.3 or lower
Swift 2.3 或更低版本
self.yourButton.backgroundColor = UIColor.redColor()
回答by Nitin Gohel
If you are working with Objective-C then this is the code for setting button background Color.
如果您正在使用 Objective-C,那么这是设置按钮背景颜色的代码。
YourButtonObject.backgroundColor = [UIColor yellowColor];
And you can direct setting background color of UIButton
from storyboard like following:
您可以直接设置UIButton
故事板的背景颜色,如下所示: