【问题标题】:Change global tint color - iOS7/iOS8更改全局色调颜色 - iOS 7/iOS 8
【发布时间】:2013-09-28 09:54:05
【问题描述】:

我们如何通过代码改变iOS7/iOS8上的全局色调?我想更改使用此属性的多个对象,但不更改每个对象,这就是我想使用全局 tint 属性的原因。

【问题讨论】:

  • 我知道您指定了“按代码”,不过,我认为重要的是要提一下情节提要的文件检查器中有一个全局 tint 属性

标签: ios7 uicolor tintcolor


【解决方案1】:

只需在您的应用程序委托中更改 UIWindowtintColor,它就会自动默认传递给它的所有 UIView 后代。

[self.window setTintColor:[UIColor greenColor]];

【讨论】:

  • 你说得对,相当简单,只需添加 _window.tintColor = [UIColor PurpleColor]; (自动合成)但是我可以从其他视图更改它吗?
  • 它在任何 UIView 上实现,因此您可以在视图层次结构中的任何视图上设置它,并且它的所有后代都将继承相同的默认 tintColor(除非您另外指定)
  • 重要提示:不要忘记测试选择器的 ios6 兼容性:if([window respondsToSelector:@selector(setTintColor:)])
  • 这个方法似乎没有传播到导航工具栏。
  • 您应该改用外观协议,正如@carmen_munich 在这里解释的那样:stackoverflow.com/a/19140595/514181
【解决方案2】:

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

【讨论】:

  • 如果您需要为 UIAlertView 按钮着色而不仅仅是主应用程序窗口,这是最佳答案!
  • UIViewtintColor 没有UI_APPEARANCE_SELECTOR 注释。这个答案是错误的。
  • 我认为这应该是正确的答案,因为它会影响 UIAlertView 而接受的答案不会。
  • @Tobol 在技术上是正确的。对此存在混淆,确实需要Apple进行澄清。 UIAppearance 在 iOS 5 中被引入作为处理全局颜色(以及更多)的一种方式,但随后在 iOS 7 中,Apple 将 tintColor 移动到 UIView 并使其传播到子视图。在iOS 7 UI Transition Guide Apple 中声明:“iOS 7 不支持使用外观代理 API 设置 tintColor 属性。”但它似乎仍然有效。
  • 谢谢杰米。我认为我们应该将此定义为未定义的行为,因为它可以在未来的 iOS 版本中更改而不会发出警告。
【解决方案3】:

有两种方法可以更改全局色调颜色。如上所述,您可以将self.window.tintColor 更改为-application:didFinishLaunchingWithOptions:

在我看来,更优雅的方法是在您的 Storyboard 的 File Inspector 中设置 Global Tint,而没有选择任何内容。这样你的-application:didFinishLaunchingWithOptions: 就更干净了。

【讨论】:

  • 任何自定义 xib 在某种程度上不受 UIWindowtintColor 属性或您建议的全局色调的影响。
  • 不确定为什么更改情节提要选项不起作用,但 self.window?.tintColor = UIColor(netHex: 0xc0392b) 起作用了。
  • 如果 Xcode 能够正常运行,那就太棒了。可能这将适用于 Xcode 版本 345。今天,正如预期的那样,Xcode 在我的头上胡扯,什么也不做。我希望 Apple 在不久的将来从开发工具负责人的位置上解雇撒旦。
  • 我觉得你是兄弟。
【解决方案4】:

您可以通过设置窗口的 tint 属性为整个应用程序指定色调颜色。为此,您可以使用类似于以下的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.tintColor = [UIColor purpleColor];
    return YES;
}

【讨论】:

    【解决方案5】:

    为 Swift 2.2 更新

    你可以在任何地方这样做:

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

    或者,如果您尝试从 AppDelegate 执行此操作,

    self.window?.tintColor = UIColor.green()
    

    【讨论】:

      【解决方案6】:

      为 Swift 5 更新

      在 App Delegate 中写入:

      self.window?.tintColor = UIColor.green
      

      【讨论】:

        【解决方案7】:

        以下事情对我不起作用

        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]
        

        以下工作:

        1. 从故事板设置全局色调颜色。

        1. 设置窗口的色调

        对于整个应用程序:

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

        对于特定控制器:

        在初始化时设置窗口的色调颜色,并在取消初始化时设置应用程序的默认色调。

            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
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-09-18
          • 1970-01-01
          • 1970-01-01
          • 2014-09-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-27
          相关资源
          最近更新 更多