swift 如何制作黑暗模式按钮

mwg9r5ms  于 2022-12-17  发布在  Swift
关注(0)|答案(1)|浏览(138)

我想在我的主屏幕上切换黑暗和光明模式的可能性。我不想用UISlider,但只是一个按钮。你点击一次,它的黑暗模式,你点击一次,它的光明。这是可能的吗?

fnx2tebb

fnx2tebb1#

UI套件

更新

更新后的版本更可靠,它检查overrideUserInterfaceStyle,如果是.unspecified,则回退到UIScreen.main.traitCollection.userInterfaceStyle

//
//  ViewController.swift
//  ToggleInterfaceStyle
//
//  Created by Sebastian on 23.09.22.
//

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Here is the button
        let toggleInterfaceStyleButton = UIButton()
        toggleInterfaceStyleButton.setTitle("Toggle Color Scheme", for: .normal)
        toggleInterfaceStyleButton.setTitleColor(.tintColor, for: .normal)
        toggleInterfaceStyleButton.frame = CGRect(x: 15, y: -50, width: 250, height: 500)
        toggleInterfaceStyleButton.addTarget(self, action: #selector(toggleInterfaceStyle), for: .touchUpInside)
        self.view.addSubview(toggleInterfaceStyleButton)
    }
    
    
    @objc func toggleInterfaceStyle() {
        let scenes = UIApplication.shared.connectedScenes
        let windowScene = scenes.first as? UIWindowScene
        let window = windowScene?.windows.first
        let interfaceStyle = window?.overrideUserInterfaceStyle == .unspecified ? UIScreen.main.traitCollection.userInterfaceStyle : window?.overrideUserInterfaceStyle
        
        if interfaceStyle != .dark {
            window?.overrideUserInterfaceStyle = .dark
        } else {
            window?.overrideUserInterfaceStyle = .light
        }
    }
}

快速用户界面

对于SwiftUI,你可以用.preferredColorScheme来改变外观(亮模式/暗模式),它改变了整个应用的外观。在这个例子中,我添加了第二个视图和一个navigationLink来显示样式保持选中状态:

import SwiftUI

struct ContentView: View {
    
    @State private var scheme: ColorScheme = .light
    @State private var isShowingNewAccountView = false
    
    func toggleScheme() {
        if scheme == .light {
            scheme = .dark
        } else {
            scheme = .light
        }
    }
    
    var body: some View {
        NavigationView() {
            VStack {
                
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundColor(.accentColor)
                    .padding()
                Text("Hello, world!")
                    .padding()
                
                
                NavigationLink(destination: SecondView()) {
                    Text("Second View")
                }
                
                Button(action: {
                    self.toggleScheme()
                }) {
                    Text("Toggle Scheme")
                }
                .padding()
            }
        }
        .preferredColorScheme(scheme)
    }
}

struct SecondView: View {
    
    var body: some View {
        
        VStack {
            
            Image(systemName: "airplane")
                .imageScale(.large)
                .foregroundColor(.accentColor)
                .padding()
            Text("Hello, second view!")
                .padding()
        }
    }
}

相关问题