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

前端 未结 4 397
既然无缘
既然无缘 2021-01-04 01:39

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

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

相关标签:
4条回答
  • 2021-01-04 02:06

    I guess you can use Method Swizzling kind some feature to change all views colour or image setting.

    Check following

    https://wiredcraft.com/blog/method-swizzling-ios/

    0 讨论(0)
  • 2021-01-04 02:07

    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.

    0 讨论(0)
  • 2021-01-04 02:12

    I don't think this is a good idea, but if you really want to do this, you could do something like this:

    - (void)setBGColor:(UIColor *)color forAllSubviewsOf:(UIView *)view
    {
        [view setBackgroundColor:color];
        for (UIView *sub in view.subviews)
            [self setBGColor:color forAllSubviewsOf:sub];
    }
    
    0 讨论(0)
  • 2021-01-04 02:24

    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.

    0 讨论(0)
提交回复
热议问题