使用开关更改 UITabBar 样式
Change UITabBar style with a switch
我正在尝试进行深色模式切换。我从本教程开始 (link),但我无法更改 tabBar 的样式。 navBar 的样式改变了,但 tabBar 保持不变。
如何在不重启应用程序的情况下更改 tabBar 的外观?
更新 #1:这是我使用的代码
import UIKit
enum Theme: Int {
case Light, Dark
var barStyle: UIBarStyle {
switch self {
case .Light:
return .Default
case .Dark:
return .Black
}
}
var backgroundColor: UIColor {
switch self {
case .Light:
return UIColor(red:0.95, green:0.95, blue:0.95, alpha:1.00)
case .Dark:
return UIColor(red:0.12, green:0.12, blue:0.12, alpha:1.00)
}
}
var cellBackgroundColor: UIColor {
switch self {
case .Light:
return UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.00)
case .Dark:
return UIColor(red:0.15, green:0.15, blue:0.15, alpha:1.00)
}
}
var labelColor: UIColor {
switch self {
case .Light:
return UIColor(red:0.12, green:0.12, blue:0.12, alpha:1.00)
case .Dark:
return UIColor(red:0.99, green:0.99, blue:0.99, alpha:1.00)
}
}
var buttonColor: UIColor {
switch self {
case .Light:
return UIColor(red:0.22, green:0.59, blue:0.95, alpha:1.00)
case .Dark:
return UIColor(red:0.22, green:0.59, blue:0.95, alpha:1.00)
}
}
var separatorColor: UIColor {
switch self {
case .Light:
return UIColor(red:0.94, green:0.94, blue:0.94, alpha:1.00)
case .Dark:
return UIColor(red:0.22, green:0.22, blue:0.22, alpha:1.00)
}
}
var tintColor: UIColor {
switch self {
case .Light:
return UIColor(red:0.62, green:0.62, blue:0.62, alpha:1.00)
case .Dark:
return UIColor(red:0.50, green:0.50, blue:0.50, alpha:1.00)
}
}
}
class ThemeManager {
static func currentTheme() -> Theme {
if(NSUserDefaults.standardUserDefaults().boolForKey("darkMode")){
return .Dark
} else {
return .Light
}
}
static func applyTheme(theme: Theme) {
if(theme == .Dark){
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "darkMode")
} else {
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "darkMode")
}
NSUserDefaults.standardUserDefaults().synchronize()
let sharedApplication = UIApplication.sharedApplication()
sharedApplication.delegate?.window??.tintColor = theme.buttonColor
UINavigationBar.appearance().barStyle = theme.barStyle
UITabBar.appearance().barStyle = theme.barStyle
UITabBar.appearance().barTintColor = theme.labelColor
UINavigationBar.appearance().tintColor = theme.labelColor
UITableViewCell.appearance().backgroundColor = theme.cellBackgroundColor
UIStepper.appearance().tintColor = theme.tintColor
UIActivityIndicatorView.appearance().tintColor = theme.tintColor
UIButton.appearance().tintColor = theme.buttonColor
UILabel.appearance().textColor = theme.labelColor
}
}
当开关状态改变时我调用ThemeManager.applyTheme(.Dark || .Light)
。
UIAppearance
代理只影响新的初始化视图。在 UIAppearance
上设置颜色和样式不会影响已经可见的视图。
您必须在 ViewController 中手动设置它:
self.tabBarController?.tabBar.barStyle = .Black
我正在尝试进行深色模式切换。我从本教程开始 (link),但我无法更改 tabBar 的样式。 navBar 的样式改变了,但 tabBar 保持不变。
如何在不重启应用程序的情况下更改 tabBar 的外观?
更新 #1:这是我使用的代码
import UIKit
enum Theme: Int {
case Light, Dark
var barStyle: UIBarStyle {
switch self {
case .Light:
return .Default
case .Dark:
return .Black
}
}
var backgroundColor: UIColor {
switch self {
case .Light:
return UIColor(red:0.95, green:0.95, blue:0.95, alpha:1.00)
case .Dark:
return UIColor(red:0.12, green:0.12, blue:0.12, alpha:1.00)
}
}
var cellBackgroundColor: UIColor {
switch self {
case .Light:
return UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.00)
case .Dark:
return UIColor(red:0.15, green:0.15, blue:0.15, alpha:1.00)
}
}
var labelColor: UIColor {
switch self {
case .Light:
return UIColor(red:0.12, green:0.12, blue:0.12, alpha:1.00)
case .Dark:
return UIColor(red:0.99, green:0.99, blue:0.99, alpha:1.00)
}
}
var buttonColor: UIColor {
switch self {
case .Light:
return UIColor(red:0.22, green:0.59, blue:0.95, alpha:1.00)
case .Dark:
return UIColor(red:0.22, green:0.59, blue:0.95, alpha:1.00)
}
}
var separatorColor: UIColor {
switch self {
case .Light:
return UIColor(red:0.94, green:0.94, blue:0.94, alpha:1.00)
case .Dark:
return UIColor(red:0.22, green:0.22, blue:0.22, alpha:1.00)
}
}
var tintColor: UIColor {
switch self {
case .Light:
return UIColor(red:0.62, green:0.62, blue:0.62, alpha:1.00)
case .Dark:
return UIColor(red:0.50, green:0.50, blue:0.50, alpha:1.00)
}
}
}
class ThemeManager {
static func currentTheme() -> Theme {
if(NSUserDefaults.standardUserDefaults().boolForKey("darkMode")){
return .Dark
} else {
return .Light
}
}
static func applyTheme(theme: Theme) {
if(theme == .Dark){
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "darkMode")
} else {
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "darkMode")
}
NSUserDefaults.standardUserDefaults().synchronize()
let sharedApplication = UIApplication.sharedApplication()
sharedApplication.delegate?.window??.tintColor = theme.buttonColor
UINavigationBar.appearance().barStyle = theme.barStyle
UITabBar.appearance().barStyle = theme.barStyle
UITabBar.appearance().barTintColor = theme.labelColor
UINavigationBar.appearance().tintColor = theme.labelColor
UITableViewCell.appearance().backgroundColor = theme.cellBackgroundColor
UIStepper.appearance().tintColor = theme.tintColor
UIActivityIndicatorView.appearance().tintColor = theme.tintColor
UIButton.appearance().tintColor = theme.buttonColor
UILabel.appearance().textColor = theme.labelColor
}
}
当开关状态改变时我调用ThemeManager.applyTheme(.Dark || .Light)
。
UIAppearance
代理只影响新的初始化视图。在 UIAppearance
上设置颜色和样式不会影响已经可见的视图。
您必须在 ViewController 中手动设置它:
self.tabBarController?.tabBar.barStyle = .Black