Ios 重新加载AppDelegate
在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
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,要求用户重新启动应用程序。