H. Ferrence
H. Ferrence

Reputation: 8106

How to set custom color in the view controller's background

I want to set the background color of my mobile app to #B2B27A.

I can get to View Controller and the Background color panel, but I cannot figure out how to set my own RGB color. Is that possible to do?

Upvotes: 0

Views: 13469

Answers (2)

Rashwan L
Rashwan L

Reputation: 38833

Simply:

self.view.backgroundColor = UIColor(red: 178/255, green: 178/255, blue: 122/255, alpha: 1)

The rgb values are equal to #B2B27A.

Edit:
You can also add an hex/rgb value in your Storyboard: enter image description here

  1. Select your view
  2. Click background in attributes inspector
  3. Select the second menu item in the colors window

Upvotes: 12

Jayesh Gyanchandani
Jayesh Gyanchandani

Reputation: 315

create a NSObject class as below:

class color:NSObject
{
    class func UIColorFromRGB(rgbValue: UInt) -> UIColor {
        return UIColor(
            red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }
}

call this class func in you ViewController viewDidLoad() like below:

self.view.backgroundColor = color.UIColorFromRGB(0xB2B27A)

Upvotes: 2

Related Questions