开发者

change the uiview background color with code

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.


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


self.view.backgroundColor = [UIColor redColor];

possible colors are :

blackColor  
 darkGrayColor  
 lightGrayColor  
 whiteColor  


 grayColor  
 redColor  
 greenColor  
 blueColor  
 cyanColor  
 yellowColor  
 magentaColor  
 orangeColor  
 purpleColor  
 brownColor  
 clearColor


For Swift 3, you should do:

self.view.backgroundColor = UIColor.white

Unfortunately, the other answers no longer work in Swift 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();


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

self.view.backgroundColor = Color Literal

change the uiview background color with code

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:

change the uiview background color with code


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜