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 UIButtonin 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 Backgroundproperty in viewsection that will allow you set the backgroundColorfor the UIButton.
只是在滚动一点点Attribute Inspector,你可以找到Background在性能view部分,可以让你设置backgroundColor的UIButton。
If you want to set backgroundColorof 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 UIButtonfrom storyboard like following:
您可以直接设置UIButton故事板的背景颜色,如下所示:


