Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView background color in Swift

Tags:

swift

xcode6

ios8

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?

like image 899
Z-Tech Avatar asked Jun 09 '14 00:06

Z-Tech


2 Answers

self.view.backgroundColor = UIColor.redColor() 

In Swift 3:

self.view.backgroundColor = UIColor.red 
like image 64
Jiaaro Avatar answered Oct 23 '22 00:10

Jiaaro


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

3) 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

4) You can successfully set the colors .

enter image description here

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

like image 36
Jaywant Khedkar Avatar answered Oct 23 '22 01:10

Jaywant Khedkar