I have tried in my viewDidLoad: in my tableview controller, But its changing for only that screen, I need to change for all my screens.

self.myTableView.backgroundColor = [UIColor purpleColor];
myTableView.opaque = NO;
3

There are 3 answers

2
Burhanuddin Sunelwala On

1.

  • Create a Singleton Class named Settings
  • Have a property for color
  • Create a BaseViewController and let all your ViewControllers inherit from this BaseViewController.
  • Set the background color on viewDidLoad: or viewWillAppear: of your BaseViewController.

Ex:

//Settings.h
@interface Settings : NSObject

+ (Settings *)sharedSettings;

@property(nonatomic, strong) UIColor *themeColor

@end

//Settings.m
+(Settings *)sharedSettings {

    static Settings *sharedSettings = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        sharedSettings = [[self alloc] init];
    });

    return sharedSettings;
}

//BaseViewController.m
#import "Settings.h"

...

- (void)viewDidLoad {

    [super viewDidLoad];
    [self.view setBackgroundColor:[Settings sharedSettings].themeColor];
}

2.

There is another method by which you can achieve this i.e. Notification Design Pattern

Whenever you change the color in settings, dispatch a notification and receive that notification in your BaseViewController.

//Settings.m
- (void)colorChanged:(UIColor *)color {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ThemeColorChanged" object:color];
}

//BaseViewController.m

- (void)viewDidLoad {

    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(@"notificationThemeColorChanged:") name:@"ThemeColorChanged" object:nil];
}

- (void)notificationThemeColorChanged:(NSNotification *)notification {

    UIColor *changedColor = notification.object;
    [self.view setBackgroundColor:changedColor];
}

For Saving

If you need to Save the Data so that whenever the user comes back to the application, he sees the last updated color, then you could save it to UserDefaults.

//Settings.h
@interface Settings : NSObject

+ (Settings *)sharedSettings;

@property(nonatomic, strong) UIColor *themeColor

@end

//Settings.m
+(Settings *)sharedSettings {

    static Settings *sharedSettings = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        sharedSettings = [[self alloc] init];
    });

    return sharedSettings;
}

- (instancetype)init {

    if(self = [super init]) {

        UIColor *savedColor = [[NSUserDefaults standardUserDefaults] objectForKey:@"ThemeColor"];
        if(savedColor) {
            self.themeColor = savedColor;
        } else {
            self.themeColor = defaultColor; //Ex:[UIColor whiteColor];
        }
    }
    return self;
}

- (void)colorChanged:(UIColor *)color {

    [[NSUserDefaults standardUserDefaults] setObject:color forKey:@"ThemeColor"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
1
Vikash Rajput On

Try this

[[UIView appearance] setBackgroundColor:[UIColor colorWithRed:150/255 green:255/255 blue:100/255 alpha:0.3f]];
1
hasan On

Without saving the background color in the user defaults. the background color will reset every time app. get closed. methods to use user defaults:

@implementation Common

+ (id) valueForKey:(NSString *)key
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    id val = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return val;
}

+ (id) valueForKey:(NSString *)key defaultValue:(id)default_value
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    id val = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    if (val)
        return val;
    [Common setValue:default_value forKey:key];
    return default_value;
}

+ (void) setValue:(id)value forKey:(NSString *)key
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:value];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end

In your master view controller. a controller which all controllers subclass:

- (void)viewWillAppear:(BOOL)Animated
{
    [super viewWillAppear:Animated];
    self.view.backgroundColor = [Common valueForKey:@"BackgroundColor" defaultValue:[UIColor whiteColor]];
}

In your settings page to save the color:

[Common setValue:[UIColor purpleColor] forKey:@"BackgroundColor"];
// or
[Common setValue:[UIColor colorWithRed:10.0/255.0 green:125.0/255.0 blue:95.0/255.0 alpha:1.0] forKey:@"BackgroundColor"];

Note that you need to set your subviews background color to clearColor if needed.

You can merge this solution @Burhanuddin Sunelwala solution to get a perfect one.