Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 477
uttam Avatar asked Jan 04 '10 08:01

uttam


People also ask

How do I change the background color in Objective C?

backgroundColor =[UIColor colorWithRed:178/255. f green:14/255. f blue:12/255. f alpha:0.05];

How do I change the background color in view controller?

First let us see using storyboard, Open Main. storyboard and add one view to the View Controller. On the right pane you can see the property, and from there update the background color to color you want your view to be as show below. Now let us see how we can change the color programmatically.


3 Answers

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];
like image 83
Liam Avatar answered Oct 19 '22 22:10

Liam


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.

like image 39
user1898829 Avatar answered Oct 19 '22 22:10

user1898829


UIViewController *yourVC;
UIColor *colour = [[UIColor alloc] initWithRed: 1.0 green: 0.0 blue: 0.0 alpha: 1.0];
[yourVC.view.backgrounColor] = colour;
[colour release];
like image 22
Frank Shearar Avatar answered Oct 19 '22 22:10

Frank Shearar