ios 使用代码更改uiview背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5525035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change the uiview background color with code
提问by Rafael
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.
我想以编程方式更改我的应用程序的背景颜色,而不是 IB。是否有可能同时获得 Swift 和 Obj-C 答案。
回答by Jamie
You can set the backgroundColor
property of whatever view it is you have on screen.
您可以设置backgroundColor
屏幕上任何视图的属性。
In Objective-C:
在 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:
在 Objective-C 中:
self.window.backgroundColor = [UIColor redColor];
In Swift:
在斯威夫特:
self.window.backgroundColor = .red
回答by user2165491
self.view.backgroundColor = [UIColor redColor];
possible colors are :
可能的颜色是:
blackColor
darkGrayColor
lightGrayColor
whiteColor
grayColor
redColor
greenColor
blueColor
cyanColor
yellowColor
magentaColor
orangeColor
purpleColor
brownColor
clearColor
回答by Semih Sezer
For Swift 3, you should do:
对于 Swift 3,你应该这样做:
self.view.backgroundColor = UIColor.white
Unfortunately, the other answers no longer work in Swift 3.
不幸的是,其他答案在 Swift 3 中不再适用。
回答by Gustavo Chavez
If you want to change the background color of the view with code in Swift, you should do instead:
如果你想用 Swift 中的代码改变视图的背景颜色,你应该这样做:
self.view.backgroundColor = UIColor.redColor();
回答by Vikas Rajput
You can use RGB Color by following code:
您可以通过以下代码使用 RGB 颜色:
UIColor *myColor = [UIColor colorWithRed:(128.0 / 255.0) green:(90.0 / 255.0)
blue:(200.0 / 255.0) alpha: 1];
self.view.backgroundcolor = mycolor;
回答by Basir Alam
For swift based project you can simply type the following and press enter:
对于基于 swift 的项目,您只需键入以下内容并按 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.
这里的 ColorLiteral 属性将为您提供默认的白色,您可以通过双击该颜色进行更改。