how to change the backgrond color of view controller from other controller in the app ?
backgroundColor =[UIColor colorWithRed:178/255. f green:14/255. f blue:12/255. f alpha:0.05];
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With