How to set button background color - xcode

How to set UIButton background UIButton in Xcode?

I see the Background and Image property in the layout property, but I can’t set anything in these two. (I can only set the image in both colors)

+7
ios iphone xcode uibutton
source share
3 answers

Just scroll a bit in the Attribute Inspector , you can find the Background property in the view section, which allows you to set the backgroundColor for UIButton .

enter image description here

If you want to programmatically set the backgroundColor buttons.

Goal 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() 
+24
source share

If you are working with Objective-C, then this is the code to set the background color of the button.

 YourButtonObject.backgroundColor = [UIColor yellowColor]; 

enter image description here

And you can adjust the background color of the UIButton from the storyboard as follows:

enter image description here

+6
source share

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 the storyboard enter image description here

+3
source share

All Articles