如何通过代码更改iOS7/iOS8上的全局色调颜色?我想更改使用此属性的多个对象,但不更改每个对象,这就是为什么我想使用全局色调属性。
只需要在您的应用程序委托中更改UIWindow
的tintColor
,它将自动传递为默认值到所有其UIView
后代。
[self.window setTintColor:[UIColor greenColor]];
UIView
上实现,因此您可以将其设置在视图层次结构中的任何视图上,并且所有后代都将继承相同的默认 tintColor
(除非您另有指定)。 - Vinzzzif([window respondsToSelector:@selector(setTintColor:)])
- MartinUIWindow
的tintColor
属性或全局色调的影响,就像另一个选项所建议的那样。设置UIView
的外观代理来设置tintColor
是有效的。 - atastrophic[[UIView appearance] setTintColor:[UIColor greenColor]];
UIView
的 tintColor
没有 UI_APPEARANCE_SELECTOR
注释。这个答案是错误的。 - Piotr TobolskiUIAppearance
在iOS 5中被引入作为处理全局颜色(等等)的一种方式,但随后在iOS 7中,苹果将tintColor
移动到了UIView
并使其传播到子视图。在iOS 7 UI Transition Guide中,苹果声明:“在iOS 7中不支持使用外观代理API设置tintColor属性。”然而,它似乎仍然可以工作。 - Jamie McDanielself.window?.tintColor = UIColor(netHex: 0xc0392b)
起了作用。 - Salah Alshaal你可以通过设置窗口的色调属性,为整个应用程序指定一种色调颜色。要做到这一点,你可以使用类似以下代码的方式:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.tintColor = [UIColor purpleColor];
return YES;
}
已更新至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()
已更新至Swift 5
在App Delegate中编写以下内容:
self.window?.tintColor = UIColor.green
for scene in UIApplication.shared.connectedScenes {
if let windows = (scene as? UIWindowScene)?.windows {
for window in windows {
window.tintColor = .green
}
}
}
以下方法对我没有用:
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]
以下方法是有效的:
或者
对于整个应用程序:
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
}