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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:55:04  来源:igfitidea点击:

How set background color of a button - xcode

iosiphonexcodeuibutton

提问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部分,可以让你设置backgroundColorUIButton

enter image description here

在此处输入图片说明

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];

enter image description here

在此处输入图片说明

And you can direct setting background color of UIButtonfrom storyboard like following:

您可以直接设置UIButton故事板的背景颜色,如下所示:

enter image description here

在此处输入图片说明

回答by RajeshKumar R

Swift 2.3

斯威夫特 2.3

    btn.backgroundColor = UIColor(red: 17.0/255.0, green: 119.0/255.0, blue: 151.0/255.0, alpha: 1.0)
    btn.backgroundColor = UIColor.redColor()

In storyboard enter image description here

在故事板中 在此处输入图片说明