-2

I want to do something like this: In Setting of app, can chose a color, after that some UI element like Navigationbar, tabbar highlight will change to that color.

Is there anyway of tut for that?

Nguyen Khoi
  • 110
  • 1
  • 8

2 Answers2

0

Here's how you'd do it in Objective-C.

- (void)setCustomizedNavigationBarStyle {


    // UIBarButtonItem styling
    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowOffset = CGSizeMake(0.0, 1.0);
    shadow.shadowColor = [UIColor clearColor];

    NSDictionary *enabledTextAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor darkGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:17.0]};

    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setTitleTextAttributes:enabledTextAttributeDictionary forState:UIControlStateNormal];

    NSDictionary *disabledTextAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:17.0]};

    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setTitleTextAttributes:disabledTextAttributeDictionary forState:UIControlStateDisabled];



    // UINavigationBarTitle styling
    NSDictionary *titleAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor blackColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:18.0]};

    [[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[UINavigationController class]]]setTitleTextAttributes:titleAttributeDictionary];

}

You could call this in didFinishLaunchingWithOptions. Once you translate to Swift, you'd add this line to didFinishLaunchingWithOptions:

setCustomizedNavigationBarStyle()

This should be readily translatable into Swift.

Adding to this, you can create a custom palette of colors. You may find this post on the topic helpful:

How do I create a category in Xcode 6 or higher?

Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180
0

You could save the color in NSUserDefaults and retrieve it by the key whenever you need to set that color to your view elements. You'll need an extension to NSUserDefaults that returns an UIColor.

Check out the accepted answer to this question.

Hope it helps!

Community
  • 1
  • 1
mohonish
  • 1,396
  • 1
  • 9
  • 21