Skip to content

Instantly share code, notes, and snippets.

@heckj
Created July 12, 2022 20:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heckj/72f713faf28014e560449f82b7ab357d to your computer and use it in GitHub Desktop.
Save heckj/72f713faf28014e560449f82b7ab357d to your computer and use it in GitHub Desktop.
SwiftUI contextual rendering example
// Content expanded from Form reference documentation at
// https://developer.apple.com/documentation/swiftui/form
import SwiftUI
enum ProfileImageSize {
case large
case medium
case small
}
enum NotifyMeAboutType {
case anything
case mentions
case directMessages
}
struct FormExample: View {
@State private var notifyMeAbout: NotifyMeAboutType = .directMessages
@State private var playNotificationSounds: Bool = false
@State private var sendReadReceipts: Bool = false
@State private var profileImageSize: ProfileImageSize = .medium
var body: some View {
NavigationView {
Form {
Picker("Notify Me About", selection: $notifyMeAbout) {
Text("Direct Messages").tag(NotifyMeAboutType.directMessages)
Text("Mentions").tag(NotifyMeAboutType.mentions)
Text("Anything").tag(NotifyMeAboutType.anything)
}
Toggle("Play notification sounds", isOn: $playNotificationSounds)
Toggle("Send read receipts", isOn: $sendReadReceipts)
Picker("Profile Image Size", selection: $profileImageSize) {
Text("Large").tag(ProfileImageSize.large)
Text("Medium").tag(ProfileImageSize.medium)
Text("Small").tag(ProfileImageSize.small)
}
Button("Clear Image Cache") {}
}
}
}
}
struct VStackExample: View {
@State private var notifyMeAbout: NotifyMeAboutType = .directMessages
@State private var playNotificationSounds: Bool = false
@State private var sendReadReceipts: Bool = false
@State private var profileImageSize: ProfileImageSize = .medium
var body: some View {
NavigationView {
VStack {
Picker("Notify Me About", selection: $notifyMeAbout) {
Text("Direct Messages").tag(NotifyMeAboutType.directMessages)
Text("Mentions").tag(NotifyMeAboutType.mentions)
Text("Anything").tag(NotifyMeAboutType.anything)
}
Toggle("Play notification sounds", isOn: $playNotificationSounds)
Toggle("Send read receipts", isOn: $sendReadReceipts)
Picker("Profile Image Size", selection: $profileImageSize) {
Text("Large").tag(ProfileImageSize.large)
Text("Medium").tag(ProfileImageSize.medium)
Text("Small").tag(ProfileImageSize.small)
}
Button("Clear Image Cache") {}
}
}
}
}
struct FormExample_Previews: PreviewProvider {
static var previews: some View {
FormExample()
VStackExample()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment