【发布时间】:2017-04-27 00:33:33
【问题描述】:
我想在我的应用中添加一个设置视图,我一直在尝试在设置首选项中更新整个应用的颜色主题。
导航栏和颜色主题似乎没有随着用户选择而改变
这是应用程序的 gif 演示: 链接 => demo app settings: theme color not updating
这是我的 AppDelegate: 我试图通过协议/委托来触发更改。没运气 ? 我相信这个问题与代表无关。委托方法正确实现。
问题来自我相信在 didFinishLaunching 之后需要重新绘制的导航栏。我遇到了这个问题(Changing navigation bar color in Swift),但到目前为止我还没有找到正确的解决方案。这很有挑战性
类 AppDelegate: UIResponder, UIApplicationDelegate, SettingsDelegate {
var lightColor:UIColor?
var darkColor:UIColor?
var darkColorLighter:UIColor?
var colorThemes:[String : AnyObject] = [String : AnyObject]()
let redColor:UIColor = UIColor(red: 192.0/255.0, green: 57.0/255.0, blue: 43.0/255.0, alpha: 1.0)
var window: UIWindow?
var defaults = UserDefaults.standard
var colorSettingsDefaults = UserDefaults(suiteName: "group.com.Links")
var THEMECOLOR:UIColor?
var TEXTCOLOR:UIColor?
var BORDERADIUS:CGFloat?
var themeSelected:Theme?
let setThemeAppearance:NSNotification.Name = NSNotification.Name("themeAppearance")
func getColorSettingsDefaults(completion:(_ theme:Theme)->()) {
if let _ = colorSettingsDefaults?.value(forKey: "ThemeAppearance") as? String {
//if color settings default avalaible
themeSelected = getTheme()
print("defaults available")
} else {
//if NO color settings default avalaible - default is DARK THEME
themeSelected = .dark
print("dark theme by default")
}
completion(themeSelected!)
}
func getTheme() -> Theme {
return colorSettingsDefaults!.value(forKey: "ThemeAppearance") as! String == "light" ? .light : .dark
}
func applyTheme(themeSelected:Theme) {
if themeSelected == . dark {
THEMECOLOR = darkColor
TEXTCOLOR = lightColor
} else {
THEMECOLOR = lightColor
TEXTCOLOR = darkColor
}
//radius
BORDERADIUS = 5.0
}
func setColorThemes() {
//application theme colors
darkColor = UIColor(red: 52.0/255.0, green: 73.0/255.0, blue: 94.0/255.0, alpha: 1.0)
darkColorLighter = UIColor(red: 52.0/255.0, green: 73.0/255.0, blue: 94.0/255.0, alpha: 0.6)
lightColor = UIColor.white
colorThemes = ["light": lightColor!, "dark": darkColor!]
}
func changeAppearance(apptheme:Theme) {
//change navigation bar title color
UINavigationBar.appearance().barTintColor = THEMECOLOR
UINavigationBar.appearance().tintColor = TEXTCOLOR
window?.rootViewController?.navigationController?.navigationItem.rightBarButtonItem?.tintColor = TEXTCOLOR
if apptheme == .dark {
//change status bar to light for the dark theme
UIApplication.shared.statusBarStyle = .lightContent
} else {
UIApplication.shared.statusBarStyle = .default
}
}
func updateAppearance(appTheme:Theme) {
UINavigationBar.appearance().barTintColor = UIColor.clear
UINavigationBar.appearance().tintColor = UIColor.clear
if appTheme == .dark {
//change status bar to light for the dark theme
UIApplication.shared.statusBarStyle = .lightContent
UINavigationBar.appearance().backgroundColor = darkColor
UINavigationBar.appearance().tintColor = lightColor
window?.rootViewController?.navigationController?.navigationItem.rightBarButtonItem?.tintColor = lightColor
UINavigationBar.appearance().barStyle = .default
} else {
UIApplication.shared.statusBarStyle = .default
UINavigationBar.appearance().backgroundColor = lightColor
UINavigationBar.appearance().tintColor = darkColor
window?.rootViewController?.navigationController?.navigationItem.rightBarButtonItem?.tintColor = darkColor
UINavigationBar.appearance().barStyle = .black
}
}
【问题讨论】:
-
appearance代理仅影响在设置appearance()属性后创建的视图。 -
我在我的代码示例中使用外观。一旦应用程序已经处于活动状态,还有其他方法可以使用外观()属性吗?感谢您为我指明正确的方向。
-
使用
appearance对所有新视图都很好。但是任何现有的视图都必须直接更新。 -
这实际上是我的问题。在切换到另一个颜色主题期间,我处于现有视图中。是否有解决方法来更新颜色(如刷新)。我试过 setLayoutIfNeeded() 和 setDisplayIfNeeded。没用?
-
您只需对当前视图执行与在
appearance代理上所做的相同的更改,但直接对视图执行。
标签: swift navigationbar