UIView background color in Swift

SwiftIos8Xcode6

Swift Problem Overview


Is there a way to set the UIView background color with Swift?

I know that in Objective-C, you would use self.view.backgroundColor = [UIColor redColor];, but that does not work the same way in Swift. I have looked around and because Swift is only about a week old, I cannot find an answer.

Does anyone have any suggestions?

Swift Solutions


Solution 1 - Swift

self.view.backgroundColor = UIColor.redColor()

In Swift 3:

self.view.backgroundColor = UIColor.red

Solution 2 - Swift

Try This, It worked like a charm! for me,

The simplest way to add backgroundColor programmatically by using ColorLiteral.

You need to add the property ColorLiteral, Xcode will prompt you with a whole list of colors in which you can choose any color. The advantage of doing this is we use lesser code, add HEX values or RGB. You will also get the recently used colors from the storyboard.

Follow steps ,

  1. Add below line of code in viewDidLoad() ,

    self.view.backgroundColor = ColorLiteral and clicked on enter button .

  2. Display square box next to =

enter image description here

  1. When Clicked on Square Box Xcode will prompt you with a whole list of colors which you can choose any colors also you can set HEX values or RGB

enter image description here

  1. You can successfully set the colors .

enter image description here

Hope this will help some one to set backgroundColor in different ways.

Solution 3 - Swift

I see that this question is solved, but, I want to add some information than can help someone.

if you want use hex to set background color, I found this function and work:

func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor {
    let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
    let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
    let blue = CGFloat(rgbValue & 0xFF)/256.0
    
    return UIColor(red:red, green:green, blue:blue, alpha:CGFloat(alpha))
}

I use this function as follows:

view.backgroundColor = UIColorFromHex(0x323232,alpha: 1)

some times you must use self:

self.view.backgroundColor = UIColorFromHex(0x323232,alpha: 1)

Well that was it, I hope it helps someone .

sorry for my bad english.

this work on iOS 7.1+

Solution 4 - Swift

You can use the line below which goes into a closure (viewDidLoad, didLayOutSubViews, etc):

self.view.backgroundColor = .redColor()

EDIT Swift 3:

view.backgroundColor = .red

Solution 5 - Swift

You can use this extension as an alternative if you're dealing with RGB value.

extension UIColor {
    static func rgb(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
        return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1)
      }
    }

Solution 6 - Swift

In Swift 4, just as simple as Swift 3:

self.view.backgroundColor = UIColor.brown

Solution 7 - Swift

The response by @Miknash and @wolfgang gutierrez barrera was helpful to me. Only difference was I had to add rgbValue: to the function call.

UIColorFromHex(rgbValue: 0xA6D632,alpha: 1 ) like so

Solution 8 - Swift

If you want to set your custom RGB color try this:

self.view.backgroundColor = UIColor(red: 20/255.0, green: 106/255.0, blue: 93/255.0, alpha: 1)

Don't forget to keep /255.0 for every color

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZ-TechView Question on Stackoverflow
Solution 1 - SwiftJiaaroView Answer on Stackoverflow
Solution 2 - SwiftJaywant KhedkarView Answer on Stackoverflow
Solution 3 - Swiftwolfgang gutierrez barreraView Answer on Stackoverflow
Solution 4 - SwiftMono.WTFView Answer on Stackoverflow
Solution 5 - SwiftTurtle0001View Answer on Stackoverflow
Solution 6 - SwiftBetterTengView Answer on Stackoverflow
Solution 7 - Swiftperson13View Answer on Stackoverflow
Solution 8 - SwiftmarcinbogielView Answer on Stackoverflow