0% found this document useful (0 votes)
10 views2 pages

SwiftUI Appearance Picker

Output

Uploaded by

bu.sushmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

SwiftUI Appearance Picker

Output

Uploaded by

bu.sushmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import SwiftUI

struct AppearanceView: View {


@Environment(\.colorScheme) var colorScheme
@State private var selectedAppearance: Appearance = .deviceSettings

enum Appearance: String, CaseIterable, Identifiable {


case light = "Light"
case dark = "Dark"
case deviceSettings = "Device Settings"

var id: String { self.rawValue }


}

var body: some View {


NavigationView {
VStack(alignment: .leading, spacing: 16) {
Text("Appearance")
.font(.title)
.bold()

Picker("Appearance", selection: $selectedAppearance) {


ForEach(Appearance.allCases) { appearance in
Text(appearance.rawValue)
}
}
.pickerStyle(MenuPickerStyle())

Spacer()
}
.padding()
.navigationBarTitle("Appearance")
.navigationBarItems(leading: BackButton())
}
.preferredColorScheme(getPreferredColorScheme())
}

private func getPreferredColorScheme() -> ColorScheme? {


switch selectedAppearance {
case .light:
return .light
case .dark:
return .dark
case .deviceSettings:
return nil
}
}
}

struct BackButton: View {


var body: some View {
NavigationLink(destination: MoreView()) {
Image(systemName: "chevron.left")
.font(.title2)
}
}
}

struct MoreView: View {


var body: some View {
Text("More")
.font(.title)
.bold()
}
}

struct AppearanceView_Previews: PreviewProvider {


static var previews: some View {
Group {
AppearanceView()
.environment(\.colorScheme, .light)
AppearanceView()
.environment(\.colorScheme, .dark)
}
}
}

You might also like