开发者

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

how to change the backgrond color of view c开发者_如何转开发ontroller from other controller in the app ?


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

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:

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.

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

Then in the secondViewController's logic

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


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.


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


For changing the background color of a view use 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.

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜