重新加载AppDelegate

时间:2017-01-21 14:14:00

标签: ios swift

在AppDelegate中我有应用颜色:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let defaults = UserDefaults.standard
    let switchState = defaults.integer(forKey: "switchState")
    if switchState == 1 {
        UINavigationBar.appearance().barTintColor = UIColor.black
        UIApplication.shared.statusBarStyle = .lightContent
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
        UITabBar.appearance().barTintColor = UIColor.black
        UISearchBar.appearance().barStyle = UIBarStyle.black
        UITextField.appearance().keyboardAppearance = UIKeyboardAppearance.dark
    }
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    return true
}

这样滑块就会打开"暗模式"。但要使更改生效,必须重新启动应用程序。如何在不重新启动应用程序的情况下执行此操作?

2 个答案:

答案 0 :(得分:1)

您只需将颜色逻辑移动到某个功能,然后在需要时调用该功能。

所以,你在班上做了这样的函数:

static func setColorsOfApp()
{
    let defaults = UserDefaults.standard
    let switchState = defaults.integer(forKey: "switchState")
    if switchState == 1 {
        UINavigationBar.appearance().barTintColor = UIColor.black
        UIApplication.shared.statusBarStyle = .lightContent
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
        UITabBar.appearance().barTintColor = UIColor.black
        UISearchBar.appearance().barStyle = UIBarStyle.black
        UITextField.appearance().keyboardAppearance = UIKeyboardAppearance.dark
    }
}

然后将此函数称为:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    YOUR_CALASS_NAME.setColorsOfApp()
    return true
}

以及您想要的任何其他地方。

答案 1 :(得分:0)

粗略浏览> wordlist = userinput.split() > id_s = {c: i for i, c in enumerate(set(wordlist), start=1)} > id_s {'code': 0, 'like': 1, 'I': 2, 'is': 3, 'to': 4, 'because': 5, 'fun': 6} > [id_s[c] for c in list] [1, 6, 7, 3, 2, 7, 3, 5, 4] 的文档会发现该函数在启动时显式运行一次。您是否考虑过将此代码移出该函数并转换为可以更自由地调用的其他方法?

Google了解application:didFinishLaunchingWithOptions:如何工作,并从回调中调用Ayush的功能,以便您的应用“立即将用户界面更改为黑暗”