objective-c 如何在objective-c中更改viewController的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1998304/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:41:13  来源:igfitidea点击:

how to change the background color of the viewController in objective-c

iphoneobjective-c

提问by uttam

how to change the backgrond color of view controller from other controller in the app ?

如何从应用程序中的其他控制器更改视图控制器的背景颜色?

回答by Liam

To change the background color of a 'view' you need to set the backgroundColor property on it. This implies that you have access to it. If it was all in one controller you would just use

要更改“视图”的背景颜色,您需要在其上设置 backgroundColor 属性。这意味着您可以访问它。如果这一切都在一个控制器中,您只需使用

self.view.backgroundColor = [UIColor redColor];

If it was in a navigation or similar based app, then you can access a views parentViewController and change the color on it as follows:

如果它在导航或类似的基于应用程序中,那么您可以访问视图 parentViewController 并按如下方式更改其颜色:

self.parentViewController.view.backgroundColor = [UIColor redColor];

If this is not possible then you can set an iVar on the second view controller when it is created that contains the instance of the viewController that you want to change the background color on.

如果这是不可能的,那么您可以在创建第二个视图控制器时在它上设置一个 iVar,其中包含要更改背景颜色的视图控制器的实例。

MyViewController* secondViewController = [[MyViewController alloc] init];
secondViewController.bgColorNeedsChangingViewController = self;

Then in the secondViewController's logic

然后在secondViewController的逻辑中

self.bgColorNeedsChangingViewController.view.backgroundColor = [UIColor redColor];

回答by user1898829

UIColor *colour = [[UIColor alloc]initWithRed:57.0/255.0 green:156.0/255.0 blue:52.0/255.0 alpha:1.0];
self.view.backgroundColor = colour;

Adapted from Frank Shearar's answer.

改编自 Frank Shearar 的回答。

回答by Frank Shearar

UIViewController *yourVC;
UIColor *colour = [[UIColor alloc] initWithRed: 1.0 green: 0.0 blue: 0.0 alpha: 1.0];
[yourVC.view.backgrounColor] = colour;
[colour release];

回答by Chandra Kanth

For changing the background color of a viewuse this single line of code

要更改视图背景颜色,请使用这行代码

self.view.backgroundColor = UIColor (red: 1.0, green: 1.0, blue: 0.5, alpha: 1.0)

The values of red, green, blue and alpha vary btw 0 to 1.

红、绿、蓝和 alpha 的值在0 到 1 之间变化。

You can also write it as

你也可以把它写成

self.view.backgroundColor = UIColor (red: 123.0/255.0, green: 200.0/255.0, blue: 90.0/255.0, alpha: 1.0)

This is in the entire range of color scheme.

这是在整个配色方案范围内。