What's new in SwiftUI for iOS 17?

Introduction

iOS 17 introduces a number of new features for SwiftUI, including -

  • Interactive widgets:  SwiftUI developers can now create interactive widgets that allow users to perform actions directly from their home screen. This is a major new feature that will make it possible to create more powerful and engaging widgets.
  • Improved animation APIs: iOS 17 includes a number of new animation APIs that make it easier to create complex and visually appealing animations. For example, there is a new phase animator modifier that allows you to create animations that can be paused and resumed at any time.
  • Expanded API coverage: The SwiftUI API library continues to grow in iOS 17, with new features for programmatic scrolling and paging, animated scroll transitions, a new Inspector component, fine-grained focus control, new keyboard input support, and more.
  • Improved data modeling with Core Data: Core Data's data modeling capabilities have been enhanced in iOS 17, offering more flexibility and expressiveness.

Interactive widgets

To create an interactive widget, you need to use the Button, or Toggle views with an AppIntent argument. The AppIntent defines the action that will be performed when the user interacts with the widget.

The following code shows a simple button widget that increments a counter:

struct CounterWidgetEntryView: View {
  var entry: CounterEntry
  @Environment(\.widgetFamily) var family

  var body: some View {
    switch family {
    case .systemSmall:
      Text("\(entry.count)")
        .font(.title)
    case .systemMedium:
      VStack {
        Text("\(entry.count)")
          .font(.title)
        Button(intent: IncrementCounterIntent()) {
          Text("Increment")
        }
      }
    default:
      Text("Not supported")
    }
  }
}

Improved animation APIs

iOS 17 includes a number of new animation APIs that make it easier to create complex and visually appealing animations.

The following code shows how to use the new phase animator modifier to create an animation that can be paused and resumed at any time -

struct PhaseAnimator: View {
  @State private var isPaused = false

  var body: some View {
    Image(systemName: "play")
      .resizable()
      .frame(width: 100, height: 100)
      .rotationEffect(Angle(degrees: isPaused ? 0 : 360))
      .phaseAnimation(isPaused ? 0 : 1)
  }
}

The isPaused state variable controls whether the animation is paused or playing. The phaseAnimation modifier takes the animation's current phase as a parameter. A phase of 0 means that the animation is at its beginning, and a phase of 1 means that the animation is at its end.

Expanded API coverage

The SwiftUI API library continues to grow in iOS 17, with new features for programmatic scrolling and paging, animated scroll transitions, a new Inspector component, fine-grained focus control, new keyboard input support, and more.

The following code shows how to use the new programmatic scrolling APIs to scroll to a specific item in a list -

struct ProgrammaticScrolling: View {
  @State private var selectedItem = 0
  private var listItems = ["Item 1", "Item 2", "Item 3"]

  var body: some View {
    List(listItems, id: \.self, selection: $selectedItem) { item in
      Text(item)
    }
    .onAppear {
      // Scroll to the selected item.
      listScrollProxy.scrollTo(item, at: .topLeading)
    }
  }
}

The listScrollProxy property provides access to the list's scroll view. The scrollTo() method scrolls to the specified item at the specified anchor point.

Improved data modeling with core data

Core Data's data modeling capabilities have been enhanced in iOS 17, offering more flexibility and expressiveness.

The following code shows how to use the new @NSManaged property wrapper to define a managed property -

@objc(Book)
class Book: NSManagedObject {
  @NSManaged public var title: String
  @NSManaged public var author: String
}

The @NSManaged property wrapper automatically generates the necessary code to manage the property's value. This makes it easier to write data models and reduces the risk of errors.


Similar Articles