Change global tint color - iOS7/iOS8

后端 未结 7 1885
暖寄归人
暖寄归人 2020-12-23 15:40

How can we change the global tint color on iOS7/iOS8 by code? I want to change multiple objects that use this property, but not change each one, that\'s why

相关标签:
7条回答
  • 2020-12-23 16:19

    Updated for Swift 5

    Write in the App Delegate :

    self.window?.tintColor = UIColor.green
    
    0 讨论(0)
  • 2020-12-23 16:21

    Following things DID NOT WORKED for me:

    navigationItem.backBarButtonItem?.tintColor = Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR
    
    navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR], for: .normal)
    
    self.navigationController?.navigationBar.barStyle = UIBarStyle.black
    
    navigationController?.navigationBar.barTintColor = Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR
    
    navigationController?.navigationBar.tintColor = Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR
    
    navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR]
    

    Following WORKED :

    1. SET THE GLOBAL TINT COLOR FROM STORYBOARD.

    OR

    1. SET THE TINT COLOR OF THE WINDOW

    FOR WHOLE APP:

    let sharedApp = UIApplication.sharedApplication()
    sharedApp.delegate?.window??.tintColor = UIColor.green()
    

    FOR SPECIFIC CONTROLLER:

    set tint color of window while initialization and set back the default tint color of the app while deinitialization.

        override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
            super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
            let window = UIApplication.shared.windows.first
            window?.tintColor = Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR
        }
    
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            let window = UIApplication.shared.windows.first
            window?.tintColor = Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR
        }
    
        deinit {
            let window = UIApplication.shared.windows.first
            window?.tintColor = Theme.light.App.DEFAULT_TINT_COLOR
        }
    
    0 讨论(0)
  • 2020-12-23 16:31

    Simply change the UIWindow 's tintColor in your application delegate, it's automatically passed as default to all its UIView descendants.

    [self.window setTintColor:[UIColor greenColor]];
    
    0 讨论(0)
  • 2020-12-23 16:32

    There are two ways to change your global tint color. As many mentioned above you could change self.window.tintColor in -application:didFinishLaunchingWithOptions:.

    More elegant way, in my opinion, is to set Global Tint in File Inspector in your Storyboard while nothing is selected. This way your -application:didFinishLaunchingWithOptions: is cleaner.

    0 讨论(0)
  • 2020-12-23 16:32

    Updated for Swift 2.2

    You can do this from anywhere like this:

    // Get app delegate
    let sharedApp = UIApplication.sharedApplication()
    
    // Set tint color
    sharedApp.delegate?.window??.tintColor = UIColor.green()
    

    Or if you're trying to do this from AppDelegate,

    self.window?.tintColor = UIColor.green()
    
    0 讨论(0)
  • 2020-12-23 16:36

    [[UIView appearance] setTintColor:[UIColor greenColor]];

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