30

How can I set the background color of a UIButton in Xcode?

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)

Nirav D
  • 71,513
  • 12
  • 161
  • 183
GMX
  • 950
  • 1
  • 14
  • 29

3 Answers3

53

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.

enter image description here

If you want to set backgroundColor of button programatically.

Objective C :

 self.yourButton.backgroundColor = [UIColor redColor];

Swift 3 and Swift 4

 self.yourButton.backgroundColor = UIColor.red

Swift 2.3 or lower

 self.yourButton.backgroundColor = UIColor.redColor()
Aamir
  • 16,329
  • 10
  • 59
  • 65
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • @ParagKadam For that select Last option in color `other`. After that in Color popup select **RGB Sliders** from drop down and you see hex color field set the hex color there will set your color to background. – Nirav D Feb 28 '17 at 11:10
9

If you are working with Objective-C then this is the code for setting button background Color.

YourButtonObject.backgroundColor = [UIColor yellowColor];

enter image description here

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

enter image description here

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
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()

In storyboard enter image description here

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70