Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS set background color for all viewcontroller self.view's

Is there a way to set same background image for all views?

Setting same background in all viewDidLoad: methods. It's not cool.

like image 391
andrusha Avatar asked Nov 15 '12 13:11

andrusha


2 Answers

You can create a custom class, let's call it a TemplateView, which is subclass of UIView. Then using xib/storyboards select the controller view and identity inspector change the Class property to 'TemplateView'.

Subsequently using an UIAppearance change the background color of the template view to the desired one.

 [[TemplateView appearance]setBackgroundColor:[UIColor blueColor]];

That will change the background color of each of the template views in your project. I think it's better solution than

[[UIView appearance] setBackgroundColor:[UIColor redColor]];

because we don't change the background of everything, just our custom class. Hope it will help.

like image 90
Janusz Chudzynski Avatar answered Oct 19 '22 09:10

Janusz Chudzynski


Yes, using UIAppearance in iOS5+:

[[UIView appearance] setBackgroundColor:[UIColor redColor]];

NOTE: UIView conforms to <UIAppearance, UIAppearanceContainer> protocols but does not mark any properties as UI_APPEARANCE_SELECTOR for some reason.

like image 34
Cfr Avatar answered Oct 19 '22 08:10

Cfr