Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios - How to change app color theme

Tags:

ios

swift

Hi I'm new to ios dev and I'm trying to make buttons, which are in my 'settings viewController', change the color theme of the entire app (i.e. background color). The code below is currently in the appDelegate class, as I found that it works great for this purpose.

So now I'm not sure how to have the buttons in my 'settings viewController' activate the code in the appDelegate. Should I be setting a listener for the event? use closures?

Any help is appreciated. please post code solutions if possible it really helps!:)

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
    Style.greenTheme()
}
like image 761
thatGuyNeil Avatar asked Oct 28 '25 05:10

thatGuyNeil


1 Answers

The solution I use:

extension UIColor{

  @nonobjc static var clr_blue: UIColor!

  static func initWithColorScheme(cs: ColorScheme){
    switch cs{
    case .Default:
      clr_blue = blueColor()
    case .Custom:
      clr_blue = UIColor(hue: 0.6, saturation: 0.85, brightness: 1, alpha: 1)
    }
  }

}

enum ColorScheme{
  case Default, Custom
}

Create as many custom colours and colour schemes as you wish. Usage:

UIColor.clr_blue

But don't forget to initialize your UIColor inside application:didFinishLaunchingWithOptions: as follows: UIColor.initializeWithColorScheme(.Custom)

To change the colour scheme of a current UIViewController without need to restart, add this method to your UIViewController:

func changeSchemeTo(cs: ColorScheme){
  UIColor.initializeWithColorScheme(cs)
  for subview in self.view.subviews{
    subview.setNeedsDisplay()
  }
}
like image 156
EBDOKUM Avatar answered Oct 29 '25 22:10

EBDOKUM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!