Ahsan Muhammad
Ahsan Muhammad

Reputation: 89

change the background color of all the viewcontrollers iphone

I need to change the background color of all the UIviewcontrollers in my app simulteniousely like choosing blue make all blue and green do the same.

Upvotes: 1

Views: 3004

Answers (4)

Sam
Sam

Reputation: 2579

The best approach to this task is to refactor your common aesthetics and view logic to a base class. For instance, in most of my projects I have a class called BaseViewController. All UIViewControllers extend from this base class. In the viewDidAppear and viewDidLoad methods I complete the appropriate setup for every view in my application. Then when you (inevitably) want to change the looks of your app, the code is only in ONE place!

Upvotes: 2

danh
danh

Reputation: 62676

Most apps view controller's are visible only one at a time. If that's true for your app, then your VCs can set their view background color before they appear.

- (void)viewWillAppear:(BOOL)animated {

    if (/* the condition that makes me supposed to have a blue background */) {

        // I was supposed to be blue already, but nobody can see me yet,
        // so everything is cool

        self.view.backgroundColor = [UIColor blueColor];
    }
}

Upvotes: 3

Ankit Srivastava
Ankit Srivastava

Reputation: 12405

One approach is to fire a notification once the color is changed and make all the viewcontrollers as the listner.. then you can change the color in selector called for that notification...

(If u want to avoid writing this whole procedure again and again you can create a base view controller subclassing the UIViewController and all the viewcontrollers will subclass this baseview controller... doing this you will have to define the listner function only once in the baseviewcontroller) .. hoping this helps... :)

Upvotes: 5

demon9733
demon9733

Reputation: 854

You cannot set the backgroundColor for UIViewController, only for it's view property. Well, you can make this UIColor value as global and when setting it, post and observer event, which will be reach to the method in each UIViewController to set its self.view.backgroundColor to specified color.

Upvotes: 2

Related Questions