Tamarisk
Tamarisk

Reputation: 939

Set default color of UI element

Let's say I wanted to set the default onTintColor globally for all of my UISwitches to red. Is there a way to set it so when I choose "default" color in the storyboard, it will appear red?

Upvotes: 1

Views: 1465

Answers (2)

pajevic
pajevic

Reputation: 4657

I am not sure if you can do this with Interface Builder, but consider using the UIAppearance class which can be used with most UI elements. For instance for UISwitch you would call:

[[UISwitch appearance] setOnTintColor:[UIColor redColor]];

This would make the tint color on all UISwitch instances to red. If you need to differentiate based on where you UI elements are you can use this code:

[[UISwitch appearanceWhenContainedIn:[MyClass1 class], [MyClass2 class], nil] setOnTintColor:[UIColor redColor]];

This code only needs to be called once somewhere (for instance your app delegate) before the UI elements are created.

Upvotes: 0

Ashish Kakkad
Ashish Kakkad

Reputation: 23882

You can set appearance from your appDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    UISwitch.appearance().tintColor = UIColor.blueColor()
    UISwitch.appearance().onTintColor = UIColor.redColor()
    return true
}

Upvotes: 1

Related Questions