2017-04-25 55 views
0

我想在我的應用中添加設置視圖,並且一直試圖在設置首選項中更新整個應用的顏色主題。更新應用設置中的導航欄顏色 - 不刷新或更新

導航欄和顏色主題似乎並不與用戶選擇

這個改變是應用程序的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 
    } 
} 
+0

的'appearance'代理隻影響一個'外觀後創建的視圖()'屬性設置。 – rmaddy

+0

我在我的代碼示例中使用了外觀。一旦應用程序已經處於活動狀態,還有另一種方式來使用appearance()屬性嗎?感謝您指點我正確的方向。 – SandyL

+0

對所有新視圖使用「外觀」是很好的。但是任何現有的視圖都必須直接更新。 – rmaddy

回答

0
in viewDidLoad() or viewDidAppear() 

    self.navigationController?.navigationBar.barTintColor = UIColor.black 
    self.navigationController?.navigationBar.tintColor = UIColor.white 
    self.navigationController!.navigationBar.titleTextAttributes = 
    [NSForegroundColorAttributeName: UIColor.white]