Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 重新加载AppDelegate_Ios_Swift - Fatal编程技术网

Ios 重新加载AppDelegate

Ios 重新加载AppDelegate,ios,swift,Ios,Swift,在AppDelegate中,我有应用程序颜色: func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let defaults = UserDefaults.standard let switchState = defaults.intege

在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
}

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

您只需将颜色逻辑移动到某个函数,然后在需要时调用该函数即可

因此,在类中创建如下函数:

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
}

只要粗略浏览一下
应用程序:didFinishLaunchingWithOptions:
的文档,就会发现该函数在启动时显式运行一次。您是否考虑过将此代码移出该函数,并转移到可以更自由地调用的其他方法中


谷歌搜索
UI开关的工作原理,并从回调中调用Ayush的函数,这样你的应用程序将“立即将UI更改为黑色”

一旦打开滑块,我希望它立即将UI更改为黑色。是的,我知道
应用程序:didFinishLaunchingWithOptions:
会做什么,现在当滑块打开时,显示一个UIAlertController,要求用户重新启动应用程序。