I would like to change the background color of my app programmatically, not IB. Is it possible to get both a Swift and Obj-C answer.
Asked
Active
Viewed 8.2k times
6 Answers
65
You can set the backgroundColor
property of whatever view it is you have on screen.
In Objective-C:
self.view.backgroundColor = [UIColor redColor];
In Swift:
self.view.backgroundColor = .red
or if it's the main window you're after,
In Objective-C:
self.window.backgroundColor = [UIColor redColor];
In Swift:
self.window.backgroundColor = .red
7
self.view.backgroundColor = [UIColor redColor];
possible colors are :
blackColor
darkGrayColor
lightGrayColor
whiteColor
grayColor
redColor
greenColor
blueColor
cyanColor
yellowColor
magentaColor
orangeColor
purpleColor
brownColor
clearColor

itsji10dra
- 4,603
- 3
- 39
- 59

user2165491
- 101
- 1
- 4
-
For reference, the list of color can be currently found at https://developer.apple.com/documentation/uikit/uicolor/1621945-clearcolor?language=objc – Cœur Mar 27 '19 at 07:58
6
For Swift 3, you should do:
self.view.backgroundColor = UIColor.white
Unfortunately, the other answers no longer work in Swift 3.

Semih Sezer
- 410
- 5
- 15
3
If you want to change the background color of the view with code in Swift, you should do instead:
self.view.backgroundColor = UIColor.redColor();

Gustavo Chavez
- 119
- 1
- 2
- 7
1
You can use RGB Color by following code:
UIColor *myColor = [UIColor colorWithRed:(128.0 / 255.0) green:(90.0 / 255.0)
blue:(200.0 / 255.0) alpha: 1];
self.view.backgroundcolor = mycolor;

Cœur
- 37,241
- 25
- 195
- 267

Vikas Rajput
- 1,754
- 1
- 14
- 26
-
Welcome to stack overflow :-) Please look at [answer]. You should provide some information why your code solves the problem. Code-only answers aren't useful for the community. – JimHawkins May 03 '17 at 09:35
-
Disagree with @JimHawkins - the problem with SO is not imperfect contribs like the above (which nevertheless have _some_ merit), it's dishearteningly critical comments, often of newbies. – Colin Stark Nov 11 '19 at 02:09
-
@ColinStark - there is at least one good reason for code-only answers: the users english is too bad, but the code is helpful as it is. But code-only answers should not be a default on SO . See also https://meta.stackoverflow.com/q/345719/1988304 . I don't see why my comment should be "dishearteningly" – JimHawkins Nov 11 '19 at 16:12
-
First you said "code-only answers aren't useful", then said "the code is helpful as it is... should not be the default". Which is it? As for disheartening: well, if a newbie is trying her/his best, makes a contribution that's non-null, and immediately gets a negative "this isn't useful" aka "worthless", they may be discouraged. This has occasionally been my experience. It's also the reputation that SO has acquired. – Colin Stark Nov 14 '19 at 07:30
1
For swift based project you can simply type the following and press enter:
self.view.backgroundColor = Color Literal
Here the ColorLiteral property will give you a default white colour which you can change by double clicking on that colour.

Basir Alam
- 1,332
- 20
- 24