Reputation: 8106
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
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:
Upvotes: 12
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