What is New in SwiftUI 4 for iOS 16
One of the highlights that Apple presented at the WWDC 2022 was iOS 16. Within this update, the new lock screen stood out, in which we can add more information and customize it to our liking. This version is fully developed with SwiftUI, which also presented a new version with interesting new features.
Let’s take a look at some of them and see what this powerful framework is all about.
What is SwiftUI?
SwiftUI takes advantage of all the power of Swift to create the graphical interfaces of our apps in a simple and innovative way. With this set of tools, we can create the views of our applications for all Apple devices. Thanks to SwiftUI we will use a code editor and a Preview area (Canvas) instead of Storyboards and Interface Builder.
It was presented at WWDC 2019 and has brought a big change in the way we create our apps. It has a declarative syntax, which means that it will be much easier to create graphical interfaces. Undoubtedly, a big change from the code that was generated with storyboards.
Requirements
To develop with SwiftUI we will need a minimum version of Xcode 11. Also, the devices where we want to run an application with SwiftUI will have to have versions equal to or higher than:
- iPhone/iPad iOS 13
- MacBook macOS Catalina 10.15
- AppleWatch WatchOS 6
- Apple TV tvOS 13
If our apps are compatible with previous versions we will not be able to use SwiftUI, and to enjoy all the new features of version 4.0 we can only do it using Xcode 14 and developing in versions equal to or higher than:
- iPhone/iPad iOS 16
- MacBook macOS Catalina 13.0
- AppleWatch WatchOS 9
- Apple TV tvOS 16
Benefits
The advantages over UIKit are quite remarkable in terms of code and layout of our applications.
We will no longer have to connect classes to views using IBActions or IBOutlets and without seeing the result of our code until we execute the app, with the cost in time that this entails. Now, thanks to the previews we will be able to see in real-time the changes in our views as we write the code, without the need to compile.
In addition, we will move from a framework with an imperative syntax to a declarative one and abandon delegates, among other aspects.
We will also have the ability to drag and drop the canvas to manually layout the graphical elements. This way the code is automatically generated, and even with SwiftUI Inspector, we can modify attributes of the elements from the canvas.
SwiftUI will not use the MVC architecture. Now, the communication between the different views is done through the Combine framework, following the MVVM architecture (Model-View-View-Model).
We will have a single centralized framework to create the UI for all supported Apple devices. The UI code will be the same for all devices. Each operating system (iOS, macOS, watchOS, and tvOS) will be in charge of displaying the visual elements adaptively on each device; we will only have to make minor tweaks for full compatibility. All this saves code and reduces UI development time without having to create them individually for each operating system.
New SwiftUI 4 Features for iOS 16
Like every year, Apple holds a developer-oriented conference called WWCD (Apple Worldwide Developers Conference), and this year it has presented many new features for SwiftUI version 4.0. Let’s take a look at some of the most important ones:
Swift Charts
This SwiftUI new framework will allow us to convert our data into informative charts and create effective and customizable charts with very little code. The framework provides us with marks, scale, axes, or legends that can be combined to create a wide range of charts.
We can create line graphs, bar charts, or scatter plots. It supports localization and accessibility functions. It also allows you to customize your charts with your own animations.
New Advanced Controls
Until now, as they did not exist in SwiftUI, some controls had to be used with UIKit; however, fortunately, Apple is migrating them little by little.
In this version we highlight the following controls:
- TexField: Data entry by a text field
- MultiDatePicker: Multiple date picker
- Toogle: Customizable and combinable checks. With the ability to combine, for example, buttons, and thus create tags with SwiftUI
- Stepper: Stepper with formatting
* Example of implementation of MultiDatePicker
Table Support
With the Table component, we can now implement tables with different rows and columns. This component offers us more power than the previous alternative, which was the lists.
Layout Grid
We will have at our disposal to layout our apps a new grid layout, that is to say, a grid of rows and columns to place our elements that, with a minimum amount of code, will make our apps much more attractive.
A SwiftUI new component to manage navigation in our apps. It will allow us, for example, to present views on the root view and easily manage the navigation status.
SF Symbols 4
Although it is not properly a new feature of SwiftUI, we feel it is important to mention it. SF Symbols is an iconography library designed to integrate seamlessly with San Francisco, the system font for Apple platforms. SF Symbols 4 has more than 700 new symbols that will help us to follow a more native design line.
If after reading some of these new features of SwiftUI 4.0 you are curious to see them in action, we recommend downloading the sample app provided by Apple in its GitHub repository.
It is a demo project showing several examples of these new features implemented in an app called Food Truck. You can see all the new features in the official Apple documentation or watch the video of the conference.