在运行时更改 UITabBar.appearance().tintColor
change UITabBar.appearance().tintColor at runtime
我想允许用户更改整个 App-Color。
但是每当我 select 一种颜色时,它只会在应用程序重新启动后才会改变。
是否有任何选项可以在运行时更改颜色?
我已经设置了一个 Button
具有这样的功能来创建一个 ColorPicker
@IBAction func colorChange(_ sender: UIButton) {
// Initializing Color Picker
let picker = UIColorPickerViewController()
// Setting the Initial Color of the Picker
picker.selectedColor = UIColor(named: "MyGreen")!
// Setting Delegate
picker.delegate = self
// Presenting the Color Picker
self.present(picker, animated: true, completion: nil)
}
当用户选择颜色时,我在此函数中进行更改
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
defaults.set(viewController.selectedColor, forKey: "myColor")
UITabBar.appearance().tintColor = viewController.selectedColor
}
为了在启动时改变颜色,我在 AppDelegate
中实现了这个
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var myColor = defaults.color(forKey: "myColor")
if myColor == nil {
myColor = UIColor(named: "MyGreen")
}
UITabBar.appearance().tintColor = myColor
return true
}
您尝试过使用 UITabBar.appearance().barTintColor
吗?
有人给了我一篇文章,让我了解如何做到这一点。
https://zamzam.io/protocol-oriented-themes-for-ios-apps/
我最终是这样做的:
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
defaults.set(viewController.selectedColor, forKey: "myColor")
apply(for: UIApplication.shared, color: viewController.selectedColor)
}
通过添加此功能
func apply(for application: UIApplication, color: UIColor) {
UITabBar.appearance().tintColor = color
application.windows.reload()
}
和这些扩展
public extension UIWindow {
/// Unload all views and add back.
/// Useful for applying `UIAppearance` changes to existing views.
func reload() {
subviews.forEach { view in
view.removeFromSuperview()
addSubview(view)
}
}
}
public extension Array where Element == UIWindow {
/// Unload all views for each `UIWindow` and add back.
/// Useful for applying `UIAppearance` changes to existing views.
func reload() {
forEach { [=12=].reload() }
}
}
我不知道它好不好 - 但它对我有用。
我想允许用户更改整个 App-Color。 但是每当我 select 一种颜色时,它只会在应用程序重新启动后才会改变。
是否有任何选项可以在运行时更改颜色?
我已经设置了一个 Button
具有这样的功能来创建一个 ColorPicker
@IBAction func colorChange(_ sender: UIButton) {
// Initializing Color Picker
let picker = UIColorPickerViewController()
// Setting the Initial Color of the Picker
picker.selectedColor = UIColor(named: "MyGreen")!
// Setting Delegate
picker.delegate = self
// Presenting the Color Picker
self.present(picker, animated: true, completion: nil)
}
当用户选择颜色时,我在此函数中进行更改
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
defaults.set(viewController.selectedColor, forKey: "myColor")
UITabBar.appearance().tintColor = viewController.selectedColor
}
为了在启动时改变颜色,我在 AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var myColor = defaults.color(forKey: "myColor")
if myColor == nil {
myColor = UIColor(named: "MyGreen")
}
UITabBar.appearance().tintColor = myColor
return true
}
您尝试过使用 UITabBar.appearance().barTintColor
吗?
有人给了我一篇文章,让我了解如何做到这一点。 https://zamzam.io/protocol-oriented-themes-for-ios-apps/
我最终是这样做的:
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
defaults.set(viewController.selectedColor, forKey: "myColor")
apply(for: UIApplication.shared, color: viewController.selectedColor)
}
通过添加此功能
func apply(for application: UIApplication, color: UIColor) {
UITabBar.appearance().tintColor = color
application.windows.reload()
}
和这些扩展
public extension UIWindow {
/// Unload all views and add back.
/// Useful for applying `UIAppearance` changes to existing views.
func reload() {
subviews.forEach { view in
view.removeFromSuperview()
addSubview(view)
}
}
}
public extension Array where Element == UIWindow {
/// Unload all views for each `UIWindow` and add back.
/// Useful for applying `UIAppearance` changes to existing views.
func reload() {
forEach { [=12=].reload() }
}
}
我不知道它好不好 - 但它对我有用。