Documents, reminders and "cheat sheets" I use in everyday development
SwiftUI uses structs for UI design, but classes for data!
var body: some View { // computed property!
⌘-⌥-p to refresh preview
@AppStorage("onboarding") var isOnboardingViewActive: Bool = false
@State private var myVar = initialValue
@FocusState private var isFocused: Bool
@Binding var myBinding: Type
@Environment(\.someName) var someName: SomeNameType // EnvironmentValues
@MainActor
@Observable
@Bindable
Text
Button
Form / Section
VStack / HStack / ZStack
NavigationStack / .navigationTitle
Slider / Stepper / Toggle
Picker
List
.navigationTitle("Title")
.onSubmit({ })
.onAppear(perform: { })
Color(.red)
.ignoresSafeArea()
LinearGradient / RadialGradient / AngularGradient
View()
.background(.indigo.gradient)
Button("Button 3") { /* Action */ }
.buttonStyle(.borderedProminent)
Button("Title", systemImage: "pencil")
Image("imageName")
Image(decorative: "imageName")
Image(systemName: "pencil")
Label("Name", systemImage: "pencil")
.padding()
.foregroundStyle(.white)
.background(.red)
Picker("Message", selection: $selection) {
Text("First").tag(0)
Text("Second").tag(1)
Text("Third").tag(2)
}
.pickerStyle(.segmented)
Stepper("\(sleepAmount.formatted()) hours", value: $sleepAmount, in: 4...12, step: 0.25)
DatePicker("Wake Up", selection: $wakeUp, in: Date.now..., displayedComponents: .date)
.labelsHidden()
Form looks very different on macOS!
List lets you use dynamic data without a ForEach.
List(0..<5) {
Text("Dynamic Row \($0)")
}
.alert(errorTitle, isPresented: $showingError) {
Button("Something") { } // Omit buttons entirely for default "OK"
} message: {
Text(errorMessage)
}
.scaleEffect(animationAmount)
.blur(radius: (animationAmount - 1) * 3)
.animation(.default, value: animationAmount)
@Observable
class MyDataClass
// Show sheet when optionalItem is non-nil
.sheet(item: $optionalItem) { item in
// Build view of item
}
@Environment(\.dismiss) private var dismiss
UserDefaults.standard.set(tapCount, forKey: "Tap")
@AppStorage("tapCount") private var tapCount = 0
Image(.example)
.resizable()
.scaledToFit()
.containerRelativeFrame(.horizontal) { size, axis in
size * 0.8
}
NavigationStack(path: $path) {
List(0..<100) { i in
NavigationLink("Select \(i)", value: i)
}
.navigationDestination(for: Int.self) { selection in
Text("You selected \(selection)")
}
.navigationTitle($title) // editable
.navigationBarTitleDisplayMode(.inline) // must be inline to edit
}
.toolbarBackground(.blue)
.toolbarColorScheme(.dark)
.toolbar(.hidden, for: .navigationBar)
.scrollBounceBehavior(.basedOnSize)
#Preview("Fixed size", traits: .fixedLayout(width: 300, height: 200)) {}
#Preview("Size that fits", traits: .sizeThatFitsLayout) {}
#Preview("Orientation", traits: .landscapeLeft) {}
struct MapAnnotationView_Previews: PreviewProvider {
static let locations: [NationalParkLocation] =
Bundle.main.decode("locations.json")
static var previews: some View {
MapAnnotationView(location: locations[0])
.previewLayout(.sizeThatFits)
.padding()
}
}
.padding()
.background(.black.opacity(0.50))
.foregroundStyle(.white)
.clipShape(Circle())
Spacer()
.containerRelativeFrame(.horizontal) { width, axis in
width * 0.6
}
In your body { }:
let _ = Self._printChanges()
init() {
UITextField.appearance().clearButtonMode = .whileEditing
}