如何设置按钮的背景颜色 - Xcode

30

如何在 Xcode 中设置 UIButton 的背景颜色?

我在布局属性中看到了 Background 和 Image 属性,但我无法在这两个属性中设置任何内容(只能在两者都设置图像而非颜色)。

3个回答

53

属性检查器(Attribute Inspector)中向下滚动一点,你就可以在视图(view)部分找到背景(Background)属性,允许你设置UIButtonbackgroundColor

enter image description here

如果你想通过编程方式设置按钮的backgroundColor

Objective-C:

 self.yourButton.backgroundColor = [UIColor redColor];

Swift 3和Swift 4

 self.yourButton.backgroundColor = UIColor.red

Swift 2.3或更低版本

 self.yourButton.backgroundColor = UIColor.redColor()

@ParagKadam,为此,请在颜色“其他”中选择最后一个选项。然后,在颜色弹出窗口中从下拉菜单中选择RGB滑块,您将看到十六进制颜色字段,将颜色设置为背景。 - Nirav D

9

如果你正在使用Objective-C,以下是设置按钮背景色的代码。

YourButtonObject.backgroundColor = [UIColor yellowColor];

enter image description here

您可以像以下这样直接从Storyboard设置UIButton的背景颜色:

enter image description here


8

Swift 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()

在故事板中 这里输入图片描述

2
啊,经典的“向下滚动”难题。谢谢Rajesh! - William T. Mallard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接