To use previews you need macOS Catalina
- Canvas shows previews by compiling and running code. You can do a lot of customizing to these previews.
- You can drag UI elements into the preview and Xcode updates the code accordingly.
- VStack and HStack are containers that act like a UIStackView.
- You can edit the UI code and in the Preview. Both update each other accordingly.
- You can use the preview editor to see and modify certain properties, like for instance alignment or text sizes.
- Methods that configure colors, fonts, etc. are called modifiers
- To show lists, you don't need delegates or datasources anymore.
- Items in a list should conform to
Identifyable
, so the list can understand which items are coming and going. - Cells in a list are self sizing by default.
- You can view and filter available modifiers in the library.
- If you want to use a list to navigate to detail views, you should add a
NavigationButton
to the list item and add subviews to the button. The navigation button also takes an instance of its destination. /TODO: curious whether this is a performance issue; do you really pass an instance as the destination?/ - Views in swiftUI are structs that conform to
View
, this means that views inherit no boilerplate and are passed by value, not reference. The framework aggressively modifies views to make an efficient render tree. - Views in Swift UI define a small part of your UI.
- A view defines its dependencies.
- When you add an
@State
annotated variable, SwiftUI takes care of the storage of that state. When the state changes, yourbody
will be accessed again and the view is rendered again. - SwiftUI detects that state belongs to a view and refreshes the rendering.
- State variables and models are the source of truth for your entire application.
BindableObject
can be used to observe changes on a model. There will be later talks to go into this- A lot of views in apps have a lot of dependencies in terms of models and other views. Lots of shared state is very hard to manage.
- User interactions, background code and completion blocks can cause a lot of problems in UI code because all events can occur in many orders and events can occur multiple times. It's very easy to have UI bugs because of this.
- Because Swift UI uses your data and model as a single source of truth, the view will always reflect the state of your app instead of depending on concurrent stuff going on. This prevents a lot of UI bugs.
- You can use Groups to provide several previews of a layout, creating a comprehensive state overview.
- You can use a forEach method to loop over items and create a view for every item in a data set, for instance to add an add button above a list of things you could create a list where the first item is static and then use a foreEach to add list items for the regular items in the list.
- You can use an
onDelete
method to implement list item deletion just like you would do in table views by updating the underlying data source when a user performs the delete action. - Previews allow you to do loads of testing without ever compiling your app.