在明暗模式之间切换 (Swift)

时间:2021-06-30 15:20:42

标签: swift uikit themes ios-darkmode darkmode

当我按下按钮时,如何在完整的应用程序中在暗模式和亮模式之间切换?我在 Xcode 12 中使用 Swift 5 和 UIKit

2 个答案:

答案 0 :(得分:1)

试试这个:

@IBAction func changeMode(_: Any) {
    let window = UIApplication.shared.windows[0]
    var mode = window.overrideUserInterfaceStyle
    mode = mode == .dark ? .light : .dark
}

答案 1 :(得分:1)

多亏了你的帮助,我现在已经做到了。

@IBAction func system(_ sender: Any) {
        let window = UIApplication.shared.keyWindow
        window?.overrideUserInterfaceStyle = .unspecified
    }
    
    @IBAction func dunkel(_ sender: Any) {
        let window = UIApplication.shared.keyWindow
        window?.overrideUserInterfaceStyle = .dark
    }
    
    @IBAction func hell(_ sender: Any) {
        let window = UIApplication.shared.keyWindow
        window?.overrideUserInterfaceStyle = .light
    }