WWDC21 SALE: Save 50% on all my Swift books and bundles! >>

What’s new in SwiftUI for iOS 15

Visual effects, remote URLs, and cleaned up APIs galore.

Paul Hudson       @twostraws

Expectations were always going to be high for SwiftUI this year, but the team didn’t disappoint – they’ve shipped a massive collection of improvements and features, including a new AsyncImage view for loading remote images, swipe actions for list rows, pull to refresh, plus shorter, simpler APIs for common uses. Alongside huge improvements to Swift itself (see What's new in Swift 5.5 for more on that), this is another significant leap forward for SwiftUI and I’m really keen to dive in.

Please keep in mind that these changes are very new – I'm pushing it as hard as I can, experimenting, refining, sharing, and learning all at the same time. If you have any feedback, please tweet me @twostraws.

You can watch the video below, or scroll down for links to articles.

Save money with our WWDC sale!

SAVE 50% To celebrate WWDC21, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

The big stuff

More welcome improvements

And there’s more…

iOS 15 lets you attach a role to your button to help SwiftUI know what kind of styling should be attached to the button. For example, if we had a Delete button we might mark it with the .destructive role so SwiftUI can highlight it in red when it makes sense:

Button("Delete", role: .destructive) {
    print("Perform delete")
}

And menus can also have a primary action, which is triggered when the menu’s button is tapped rather than held down – press and release to trigger the primary action, or hold down to get the full menu of options.

So, we could create a menu that supports both simple taps as well as a full set of options:

struct ContentView: View {
    var body: some View {
        Menu("Options") {
            Button("Order Now", action: placeOrder)
            Button("Adjust Order", action: adjustOrder)
            Button("Cancel", action: cancelOrder)
        } primaryAction: {
            justDoIt()
        }
    }

    func justDoIt() {
        print("Button was tapped")
    }

    func placeOrder() { }
    func adjustOrder() { }
    func cancelOrder() { }
}

Plus, there are stacks of improvements that have come about thanks to improvements in Swift itself. You can find out more in my article What’s New in Swift 5.5, but here’s the abridged version.

First, styles now use much simpler names: SidebarListStyle() becomes just .sidebar, and RoundedBorderTextFieldStyle() becomes just .roundedBorder.

Second, you can now use #if for postfix member expressions, like this:

Text("Welcome")
#if os(iOS)
    .font(.largeTitle)
#else
    .font(.headline)
#endif

And third, Swift 5.5 is able to implicitly convert between CGFloat and Double in most places where it is needed, which means you can pretty much remove CGFloat from your SwiftUI code.

And we’re not finished yet!

I think I’ve only covered about half the iOS 15 changes in SwiftUI so far, so come back later and you’ll almost certainly find this article updated with extra links and examples.

Thanks to the SwiftUI team for doing such an incredible job this year, and to Apple’s developer publications team for working so hard to create fantastic documentation we can all learn from!

Save money with our WWDC sale!

SAVE 50% To celebrate WWDC21, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

About the author

Paul Hudson is the creator of Hacking with Swift, the most comprehensive series of Swift books in the world. He's also the editor of Swift Developer News, the maintainer of the Swift Knowledge Base, and a speaker at Swift events around the world. If you're curious you can learn more here.

Was this page useful? Let us know!

Average rating: 4.8/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.