常用的宏方法
@main
The @main attribute identifies the app’s entry point.
1 | import SwiftUI |
The entry point is responsible for the start up of the app.
The MyApp structure conforms to the App protocol
,
The structure implements the computed property body
, which is a requirement of the App protocol
.
SwiftUI provides different types of scenes including WindowGroup
, Window
, DocumentGroup
, and Settings
@Observable
@State
State is a value, or a set of values, that can change over time, and that affects a view’s behavior, content, or layout.
@Binding
A binding acts as a reference to a mutable state. When a user taps the toggle from off to on, and off again, the control uses the binding to update the view’s state accordingly.
@Environment
The property gets its value automatically, as long as the environment(_:)
modifier has been applied to a parent.
The @Environment
property wrapper enables you to read the model data of the current view.
@Bindable
常用控件和概念
View protocol
1 | import SwiftUI |
-
ContentView
implements the computed propertybody
-
Text
a view that displays one or more lines of read-only text. -
ContentView applies the
padding(_:_:)
modifier to the VStack,that is,space — to the edges of the VStack viewyou can specify which edges and amount of padding to apply by providing edges and length parameter values;
padding([.bottom, .trailing], 20).
modifiers 修饰符
To customize a SwiftUI view, you call methods called modifiers
Modifiers wrap a view to change its display or other properties.
Each modifier returns a new view, so it’s common to chain multiple modifiers, stacked vertically.
1 | import SwiftUI |
Spacer
A spacer expands to make its containing view use all of the space of its parent view, instead of having its size defined only by its contents.
padding
the padding() modifier to give the View a little more space around it outer edges.
Codable
Codable conformance makes it easier to move data between the structure and a data file.
List or ForEach
To combine static and dynamic views in a list, or to combine two or more different groups of dynamic views, use the ForEach type instead of passing your collection of data to List.
1 | var body: some View { |
1 | var body: some View { |
Canvas Skill
- The canvas displays previews in
Live mode
by default so that you can interact with them - you can use the
Selectable mode
to enable editing instead.
1 | import SwiftUI |
open Show SwiftUI Inspector
In the preview,
Command-Control-click
the greeting to bring up the structured editing popover, and choose “Show SwiftUI Inspector”.open the inspector by
Control-clicking
on the Text declaration in the code editor, and then choose “Show SwiftUI Inspector” from the popover.
Interfacing with UIKit
SwiftUI works seamlessly with the existing UI frameworks on all Apple platforms. For example, you can place UIKit views and view controllers inside SwiftUI views, and vice versa.
Create a view to represent a UIPageViewController
- To represent UIKit views and view controllers in SwiftUI, you create types that conform to the
UIViewRepresentable
andUIViewControllerRepresentable
protocols.
SwiftUI manages their life cycle and updates them when needed.
1 | import SwiftUI |
- add the two requirements for the
UIViewControllerRepresentable
protocol.
1 | func makeUIViewController(context: Context) -> UIPageViewController { |
SwiftUI calls this method a single time when it’s ready to display the view, and then manages the view controller’s life cycle.
1 | func updateUIViewController(_ pageViewController: UIPageViewController, context: Context) { |
Create the view controller’s data source
- Declare a nested Coordinator class inside PageViewController.
1 | class Coordinator: NSObject { |
SwiftUI manages your UIViewControllerRepresentable type’s coordinator, and provides it as part of the context when calling the methods you created above.
- Add another method to PageViewController to make the coordinator.
1 | func makeCoordinator() -> Coordinator { |
SwiftUI calls this makeCoordinator() method before makeUIViewController(context:), so that you have access to the coordinator object when configuring your view controller.
- Tip
You can use this coordinator to implement common Cocoa patterns, such as delegates, data sources, and responding to user events via target-action.