Author: Inclined Han, iOS developer, currently working at Alibaba, iOS architecture group

Session:developer.apple.com/videos/play…

An overview of the

This topic focuses on the core concepts of building apps using SwiftUI. This year, SwiftUI expanded its declarative API to include larger Scenes such as Scenes and Apps. With these apis, The entire APP can be written without importing platform framework (UIKIt/APPKit). The following parts will be explained in detail.

Views scenes and apps

Views

What is View in SwiftUI? A View represents a part of the user interface. In an App, all visible parts are views, such as an image or text, and the container that hosts them. In fact, every pixel you see on the current device is part of a View.

scenes

But not all views on the current device’s screen come from the same app, and apps can’t directly control the entire screen. The operating system controls the specific areas that apps can display on the screen. These specific areas are called scenes in SwiftUI.

Window is the most common Scene. Some devices, such as iPad, can display multiple Windows in a split screen at the same time, while some devices, such as iOS tvOS WatchOS, have only one full-screen Window.

MacOS can display multiple Windows in different styles. Different Windows can show different scenes.

MacOS also supports merging multiple related Windows into a group, in which multiple scenes are grouped together by a single TAB, but the shared Window also has its own Scene and displays selected subscenes.

apps

The combination of scenes mentioned earlier forms the App and the current concept App Scene View forms a tree structure.

Views are the basic element for building visible parts. They can also be combined to build complex UI. Views are combined into specific scenes and can be displayed independently on a specific platform, for example, they can be assigned to display or aggregated into a TAB page.

The Demo show

The above very simple code describes the components of the entire APP, and you can see that each component corresponds to the APP Scene View mentioned above. In addition, the state management and description methods proposed by SwiftUI 1.0 are also basically synchronized to the Scene to maintain consistency.

Refer to type-based Program Entry Points for @main annotations

What are the Scenes in SwiftUI?

What does WindowGroup do?

The code looks pretty simple, and you’ll be wondering how SwiftUI does it. Here’s a detailed description of the magic in SwiftUI. In the code above, there’s a core data structure called WindowGroup. On the iPad, WindowGroup gives you the ability to automatically manage multiple Windows. You can create multiple Windows using App Expose, And the states of multiple Windows do not affect each other.

Here is a core concept in SwiftUI. App can provide a shared Model for scenes, but the state of views in different scenes is independent. By the way, navigationBarTitle Modifier in SwiftUI can also be displayed on the App selection page. This special Modifier can affect the parent Scene. Multi-scene is a very common phenomenon on macOS. Generally, APP will provide the ability to create a new Scene through the shortcut key CMD +N. Multiple Windows can be created or merged through the menu bar of Mac. Displaying the title through navigationTitle lets the user know which window they are working on.

The ability to create multiple Windows on an iPad or MAC is automatically provided by Windows Group.

How does WindowGroup work?

A WindowGroup is a Scene that can show the main interface of the App and show the View defined by the user in the code. And WindowGroup is compatible with all Apple platforms,

The life cycle of the Scene is automatically managed by the corresponding platform.

Taking macOS as an example, WindowGroup can instantiate multiple scenes, and these different scenes can individually respond to user events. Although each Scene comes from the same UI definition, user behavior at different times will also lead to different states of different scenes.

A new Property Wrappe SceneStrorage is introduced to save the state of different scenes easily, and SwiftUI helps you automate persistent state management.

Customize Scene in App

In addition to Windows Group, SwiftUI also provides DocumentGroup based APP. Sencee supports easy document manipulation on MAC iPad devices. You can refer to Build Document-based Apps in SwiftUI Session for this project. On the MAC OS platform, we support a separate Setting Scene, which can be automatically associated with App preference Settings.

Also supports shortcut modifier using declarative syntax to add shortcuts the App wants to support.

The above article only introduces a very brief part of SwiftUI’s new additions. It is recommended that readers read Data Essentials in SwiftUI to learn how to correctly transfer Data between Views using Data flow primitives provided by SwiftUI. Learn about What’s new in Swift like the Switch Case statement in Function Builder and other features that enhance SwiftUI.

Recommended reading

✨ Details the major changes and core strengths of WWDC 20 SwiftUI

WWDC20 10041 – What’s new in SwiftUI

WWDC20 10048 – Create complex features in SwiftUI

WWDC20 10039 – How to write a standalone App with SwiftUI?

WWDC20 10033 – Build SwiftUI view for widgets

Limited welfare

This article is from WWDC20 Inside Reference. I recommend this column here.

The “WWDC Insider reference” series was initiated by the veteran Driver Weekly, Knowledge Collection and SwiftGG technology organizations. We’ve been doing it for a couple of years, and it’s been good. It is mainly for the annual WWDC content, do a selection, and call on a group of front-line Internet iOS developers, combined with their actual development experience, Apple documents and video content to do a second creation.

There are 213 sessions this year. WWDC20 Insider selected 135 of these sessions, and the column has produced more than 100 articles. It is on sale now, only 29.9 yuan, very discount.

See article also not enough friends, to subscribe to the WWDC20 inside ~ https://xiaozhuanlan.com/wwdc20 to continue reading

Pay attention to our

We launched LSJCoiding, a weekly newsletter for veteran drivers, which is called a welcome update to each issue.