WWDC 2020 has ended. As usually, Apple introduced a lot of new things during the conference. They include the transition from Intel chips to ARMs, new iMacs, new OSs. One of the most expected announcements also happened: SwiftUI 2.0. Let’s discover if there is anything new in this version of SwiftUI, which starts to mature nicely.

1. A new way to set the root view

In Swift 5.3, we have a new type call – a type-based program execution. The keyword of this type that you can see is @main. We will dive deeper into this attribute as well as the new type in another article.

For the scope of this article, you just need to know that Apple leveraged @main to create a clean way to define the root view. There is no more AppDelegate and SceneDelegate

@main
struct AppTemplateView: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

2. TextEditor

In addition to the TextField component that appeared earlier, Apple has released a built-in TextView which multiple lines input and is scrollable. The only surprise is why didn’t it come sooner.

TextEditor(text: $fullText)

3. Link and Label

These new things are just the combination of a label and another support component. For example:

With Link, you can redirect to a browser by clicking on it

Link("iOS App Template", destination: URL(string: "https://iosapptemplates.com")!)

With Label, you can add system images or icons alongside text

Label("iOS App Template", systemImage: "up.icloud")

4. Map and ProgressView

As expected, Apple will gradually integrate the views which they miss in SwiftUI 1.0 into built-in SwiftUI components. So now, we don’t need to wrap MapKit from UIKit or create our own ProgressView by using ZStack anymore.

ProgressView("Progress", value: 10, total: 100)
Map(mapRect:interactionModes:showsUserLocation: userTrackingMode:

5. Dynamic Font Type

This new feature helps Text shrink or grow as needed, especially in runtime. Now, besides providing a specific font and size, the text still can scale up or down.

Text("iOS App Template")
 .font(.custom("Roboto", size: 14, relativeTo: .headline))

Some dynamic font types you can refer such as largerTitle, headline, etc.

6. LazyVStack, LazyHStack

In the previous version, when we use VStack and HStack in ScrollView, they will load all contents upfront which is likely to cause performance and memory issues when rendering large amounts of data. 

In SwiftUI 2.0, we had a new way to load content when needed to solve these issues above: LazyVStack, LazyHStack. Now, a view is only loaded when scrolling to it

struct ContentView: View {
    var body: some View {
        ScrollView(.vertical) {
            LazyHStack(spacing: 10) {
                ForEach(0..<100) { index in
                    Text("\(index)")
                }
            }
        }
    }
}

7. onChange() modifier

You can call this modifier on any views

TextEditor(text: $text)
    .onChange(of: clearText) { value in
         if !clearText {
            currentText = value
         }
     }

This performs an action when a state changes. This is one of the important improvements in this release because we can’t use property observers with @State in SwiftUI 1.0.

Conclusion

Here is a summary of the features that in our opinion are most noticeable. In addition, SwiftUI 2.0 also has other features such as ColorPicker, VideoPlayer, ScrollViewReader, SignInWithAppleButton, etc. These features are quite easy to understand: just by the name you can guess their features.

Additionally, in this phase, Apple also released a few built-in wrapper properties such as AppStorage.

Overall, not too many improvements really stand out. Most of the components are integrated into the built-in view. Some refactoring also happened, breaking legacy APIs. We still haven’t seen Apple mention the NavigationLink issues or the View lifecycle issues, unfortunately.

We will continue to analyze and learn more about SwiftUI 2.0 and see if these problems have been solved. But until then, we are still not going to use SwiftUI in production.


Leave a Reply

Your email address will not be published. Required fields are marked *