Last updated 8 min read

What's new in SwiftUI – WWDC2022

Charts framework, brand new and fixed NavigationStack. Multi-window and menu bar extras for MacOS SwfitUI apps. New Layout protocol and many other things. Here is a detailed summary of the "What's new in SwiftUI" session from WWDC2022.
#iOS App Development

Swift Charts

Charts is a new framework for building data-driven charts. Since Charts conform to View protocol all charts can be seamlessly integrated into SwiftUI-powered UI.

/content/images/2022/07/Screenshot-2022-07-02-at-15.42.04.png

Looks very easy to use and customize.

Bar chars:

/content/images/2022/07/Screenshot-2022-07-02-at-15.42.55.png

Line charts:

/content/images/2022/07/Screenshot-2022-07-02-at-15.43.52.png

Allows to configure symbols, annotations

/content/images/2022/07/Screenshot-2022-07-02-at-15.46.46.png

and even combine several charts into one using ForEach:

/content/images/2022/07/Screenshot-2022-07-02-at-15.50.25.png

Charts also support dynamic type, dark mode and localization automatically and are available for all the platforms.

/content/images/2022/07/Screenshot-2022-07-02-at-15.50.16.png

Recommended to watch:

  • Hello Swift Charts - WWDC22
  • Swift Charts: Raise the bar - WWDC22
/content/images/2022/07/Screenshot-2022-07-02-at-16.45.34.png

Not without problems, but SwiftUI has already been capable of common navigation patterns like push and pop navigation stacks, split views for list -> detail navigation pattern, and multi-window modes for MacOS

This year Apple introduced a brand new container called NavigationStack for push-pop style navigation.

/content/images/2022/07/Screenshot-2022-07-02-at-16.46.58.png

Looks almost exactly like its predecessor - NavigationView with NavigationLink.

However, data-driven navigation now looks a little bit different and uses navigationDestination modifier.

/content/images/2022/07/Screenshot-2022-07-02-at-16.52.16.png

NavigationView used to be 100% state-driven with a binding to a bool value that controlled the whole presentation. Under the hood, it used plain old good UINavigationViewController.

I played a lot with NavigationView before and I even tried to implement my own and I always had troubles.

There was a lot of pain related to keeping the animated asynchronous navigation stack presentation in sync with the state. I tried a couple of times and the whole thing was so ugly and buggy that I gave up.

From what I see now from the API, it looks like Apple did the same and moved from state-driven navigation to actions/event-driven.

NavigationLink is likely to handle a tap like a button and pass the item right to the closure from navigationDestination, then it gets the destination view, wraps it into a UIHostingVIewController, and pushes it to the navigation stack.

The example batch navigation looks very interesting and allows to jump back to any depth of the navigation stack.

/content/images/2022/07/Screenshot-2022-07-02-at-17.13.29.png

I'm interested in how it would cope with a presentation of a deep stack of items at once.

Split Views

NavigationSplitView is a brand new container for list -> detail presentation. It's rather powerful. First of all, it supports two and three-column layouts.

/content/images/2022/07/Screenshot-2022-07-04-at-19.51.37.png

It uses NavigationLink to bind the selected value that is later used for detail view presentation:

/content/images/2022/07/Screenshot-2022-07-04-at-19.57.26.png

NavigationSplitView automatically collapses into a NavigationStackView for compact size classes.

/content/images/2022/07/Screenshot-2022-07-04-at-19.58.52.png

NavigationSplitView and NavigationStackView can be nested one into another allowing to build complex navigation structures for MacOS and iPadOS:

/content/images/2022/07/Screenshot-2022-07-04-at-20.00.23.png
/content/images/2022/07/Screenshot-2022-07-04-at-20.02.37.png

Recommended to watch:

  • The SwiftUI cookbook for navigation - WWDC2022

Scenes

Window

Additionally to the WindowGroup,

/content/images/2022/07/Screenshot-2022-07-04-at-20.04.40.png

Window has been introduced this year, allowing to build multi-window apps with SwiftUI.

/content/images/2022/07/Screenshot-2022-07-04-at-20.05.00.png

Window allows to add a keyboard shortcut to jump between windows just in one line:

/content/images/2022/07/Screenshot-2022-07-04-at-20.05.15.png

Now there is a new way to open Windows programmatically with a help of a new openWidnow environment var. For example on a tap of a button:

/content/images/2022/07/Screenshot-2022-07-04-at-20.09.07.png

Window is shipped with new modifiers allowing to specify default new window positions and size:

/content/images/2022/07/Screenshot-2022-07-04-at-20.09.25.png

Here is a "real-life" multiplatform example showing how a view presented as sheet on iOS will be presented as a separate Window when an app is run on MacOS:

/content/images/2022/07/Screenshot-2022-07-04-at-20.14.24.png

Xcode now has multiplatform deployment targets to handle all this stuff for apps built with SwiftUI.

/content/images/2022/07/Screenshot-2022-07-04-at-20.16.11.png

Recommended to watch:

  • [[What's new in Xcode - WWDC22]]
  • [[Use Xcode to develop a multiplatform app - WWDC22]]

Now, menu bar items can be built entirely in SwiftUI:

/content/images/2022/07/Screenshot-2022-07-04-at-20.25.20.png

Actually, MenuBarExtra allows building the whole app as a menu bar item.

Recommended to watch:

  • [[Bring multiple windows to your SwiftUI app - WWDC22]]

Advanced controls

/content/images/2022/07/Screenshot-2022-07-04-at-20.41.13.png

Forms

MacOS settings app can be a good example of a neat design of a complicated form. For that purpose, new grouped form style can be used and it will automatically adapt the controls to the new style on MacOS:

/content/images/2022/07/Screenshot-2022-07-04-at-20.42.51.png

Section would automatically group the set of items:

/content/images/2022/07/Screenshot-2022-07-04-at-20.45.57.png

while the items within the section would be properly aligned:

/content/images/2022/07/Screenshot-2022-07-04-at-20.46.17.png

LabeledContent

LabeledContent allows applying the new style to the content controls. New controls can be built with the use of it, or it can be used for read-only info:

/content/images/2022/07/Screenshot-2022-07-04-at-20.48.54.png

The same form view can be used on iOS with its picker controls adopted to iOS style automatically:

/content/images/2022/07/Screenshot-2022-07-04-at-20.53.38.png

And it also applies proper styling for iPadOS, allowing to share code efficiently between the platforms.

/content/images/2022/07/Screenshot-2022-07-04-at-21.00.39.png

Controls

TextFields

Multiline TextFields that may expand vertically according to specified number of line limit parameters.

/content/images/2022/07/Screenshot-2022-07-07-at-15.35.29.png

MultidatePicker

MultidatePicker - yes, please!

/content/images/2022/07/Screenshot-2022-07-07-at-15.38.05.png

Mixed-state controls

Aggregate toggle may take a collection of bool bindings as an input. In the example below, DisclosureView shows individual toggles or collapses into a single aggregate toggle:

/content/images/2022/07/Screenshot-2022-07-07-at-15.47.07.png

Aggregate picker works in a similar way:

/content/images/2022/07/Screenshot-2022-07-07-at-15.40.42.png

ButtonStyle modifier

Button style modifier can be applied to all the views in the hierarchy that support button styles: toggles, menus, pickers:

/content/images/2022/07/Screenshot-2022-07-07-at-17.13.40.png

Steppers

Now steppers support value format:

/content/images/2022/07/Screenshot-2022-07-07-at-17.17.36.png

and now they are also available on WatchOS:

/content/images/2022/07/Screenshot-2022-07-07-at-17.19.03.png

Accessibility quick actions

accessibilityQuickAction is a new feature allowing to define an action that is performed when the user clenches his hand:

/content/images/2022/07/Screenshot-2022-07-07-at-17.23.29.png

Tables

Tables are now available for iPadOS

/content/images/2022/07/Screenshot-2022-07-07-at-17.26.58.png

Tables are also presented properly for smaller screen size classes, or on iOS. They simply collapse into the primary column:

/content/images/2022/07/Screenshot-2022-07-07-at-17.28.17.png

While on MacOS the table appearance would be like this:

/content/images/2022/07/Screenshot-2022-07-07-at-17.29.44.png

Context Menu Selection

Here is how the context menu selection handler modifier can be added to a Table:

/content/images/2022/07/Screenshot-2022-07-07-at-17.30.55.png

For example, it allows to handle the number of selected rows and present different context menus depending on it.

/content/images/2022/07/Screenshot-2022-07-07-at-17.34.44.png

Toolbar Items

iPadOS has a new toolbar design allowing to add ToolbarItems to it:

/content/images/2022/07/Screenshot-2022-07-07-at-17.37.08.png

and provide reordering capabilities:

/content/images/2022/07/Screenshot-2022-07-07-at-17.37.27.png

The same API is available for MacOS.

For compact size classes ToolbarItems are available in the overflow menu.

Search tokens

Search modifier now support tokens that allow to present search suggestions.

/content/images/2022/07/Screenshot-2022-07-07-at-17.44.09.png

Search scopes

Search scopes are intended to help with filtering search results. They appear on the scope bar below the toolbar on MacOS:

/content/images/2022/07/Screenshot-2022-07-07-at-17.44.43.png

or look like a segmented control on iOS:

/content/images/2022/07/Screenshot-2022-07-07-at-17.50.30.png

Recommended to watch:

  • [[SwiftUI on iPad: Organize your interface - WWDC2022]]
  • [[SwiftUI on iPad: Add toolbars, and documents and more - WWDC2022]]
  • [[What's new in iPad app design - WWDC2022]]
  • [[SwiftUI on the Mac: Build the fundamentals - WWDC2021]]

Sharing

Photos

New multiplatform privacy-focused APIs for picking photos and videos. with the help of PhotosPicker view:

/content/images/2022/07/Screenshot-2022-07-07-at-17.56.52.png

Sharing

Each platform including WatchOS now supports its own content-sharing UI:

/content/images/2022/07/Screenshot-2022-07-07-at-18.00.05.png

Here is an example of how to use it by adding ShareLink into a toolbar:

/content/images/2022/07/Screenshot-2022-07-07-at-18.01.34.png

or a contextMenu:

/content/images/2022/07/Screenshot-2022-07-07-at-18.03.24.png

Transferable

Photos picker, sharelink, and drag-n-drop capabilities are powered with a new Transferable protocol. It describes how types are transferred and allows to share data between apps.

Here is an example of how image drag-n-drop can be implemented with a newdropDestination API:

/content/images/2022/07/Screenshot-2022-07-07-at-18.09.25.png

String, Data, URL, AttributedString, and Image already conform to Transferrable and it's also possible to make transferable your own types:

/content/images/2022/07/Screenshot-2022-07-07-at-18.10.35.png

Recommended to watch:

  • [[Meet Transferable - WWDC2022]]

Graphics and layout

Shape Style

Color has now gradient property that works with system colors. Shadow modifier for Color allows to apply shadows to text and symbols including the whole set of SF symbols.

Previews

Previews now support variances to tune appearances, size classes, and orientation.

Text animations

Text font weight, style, and layout can now be animated.

/content/images/2022/07/Screenshot-2022-07-07-at-18.55.59.png

Layout

SwiftUi got a huge update this year deserving a separate session devoted to it.

Grid

Grid is a new container view allowing to build, well... grids!

/content/images/2022/07/Screenshot-2022-07-07-at-18.57.23.png

Layout protocol

Brand new layout protocol promises to lift complex layouts to an absolutely new level.

/content/images/2022/07/Screenshot-2022-07-07-at-19.02.15.png

Recommended to watch:

  • [[Compose custom layouts with SwiftUI - WWDC2022]]

References

What's new in SwiftUI