change the uiview background color with code

2019-04-18 01:31发布

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.

6条回答
Emotional °昔
2楼-- · 2019-04-18 01:57

If you want to change the background color of the view with code in Swift, you should do instead:

self.view.backgroundColor = UIColor.redColor();
查看更多
We Are One
3楼-- · 2019-04-18 02:01

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;
查看更多
smile是对你的礼貌
4楼-- · 2019-04-18 02:08

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
查看更多
看我几分像从前
5楼-- · 2019-04-18 02:13
self.view.backgroundColor = [UIColor redColor];

possible colors are :

blackColor  
 darkGrayColor  
 lightGrayColor  
 whiteColor  


 grayColor  
 redColor  
 greenColor  
 blueColor  
 cyanColor  
 yellowColor  
 magentaColor  
 orangeColor  
 purpleColor  
 brownColor  
 clearColor
查看更多
来,给爷笑一个
6楼-- · 2019-04-18 02:14

For Swift 3, you should do:

self.view.backgroundColor = UIColor.white

Unfortunately, the other answers no longer work in Swift 3.

查看更多
Explosion°爆炸
7楼-- · 2019-04-18 02:18

For swift based project you can simply type the following and press enter:

self.view.backgroundColor = Color Literal

enter image description here

Here the ColorLiteral property will give you a default white colour which you can change by double clicking on that colour.

You can choose from a list of colour from this popup: enter image description here

查看更多
登录 后发表回答