How can I add a navigation split view

Generated on 8/2/2024

1 search

To add a navigation split view in SwiftUI, you can use the existing NavigationSplitView APIs. Here are some steps and tips based on the content from WWDC:

  1. Using NavigationSplitView:

    • You can build a navigation split view using the NavigationSplitView API. This is particularly useful for creating sidebars that help users navigate through your app's content.
  2. Adopting Sidebar Appearance:

    • If you are currently using a tab bar, you can easily switch to a sidebar appearance by setting the tab view style to sidebarAdaptable. This can be done with just one line of code:
      .tabViewStyle(.sidebarAdaptable)
      
    • This modifier will transform your tab bar into a sidebar, which is especially useful on iPadOS and tvOS.
  3. Customization and Flexibility:

    • The sidebar in iOS 18 and iPadOS 18 has become more flexible, allowing users to switch between a tab bar and a sidebar representation. This can be particularly useful for apps with a lot of content, as it allows for customization such as reordering and removing tabs programmatically.
  4. Example Code:

    • Here is a simple example of how you might set up a navigation split view in SwiftUI:
      import SwiftUI
      
      struct ContentView: View {
          var body: some View {
              NavigationSplitView {
                  List {
                      NavigationLink("Item 1", destination: Text("Detail 1"))
                      NavigationLink("Item 2", destination: Text("Detail 2"))
                  }
                  .navigationTitle("Sidebar")
              } detail: {
                  Text("Select an item")
              }
          }
      }
      
      struct ContentView_Previews: PreviewProvider {
          static var previews: some View {
              ContentView()
          }
      }
      

For more detailed information, you can refer to the session Elevate your tab and sidebar experience in iPadOS (06:41).

Relevant Sessions

  1. Elevate your tab and sidebar experience in iPadOS
  2. What’s new in SwiftUI
  3. Migrate your TVML app to SwiftUI

These sessions provide comprehensive insights into using and customizing navigation split views and sidebars in SwiftUI.

Xcode essentials

Xcode essentials

Edit, debug, commit, repeat. Explore the suite of tools in Xcode that help you iterate quickly when developing apps. Discover tips and tricks to help optimize and boost your development workflow.

What’s new in SwiftUI

What’s new in SwiftUI

Learn how you can use SwiftUI to build great apps for any Apple platform. Explore a fresh new look and feel for tabs and documents on iPadOS. Improve your window management with new windowing APIs, and gain more control over immersive spaces and volumes in your visionOS apps. We’ll also take you through other exciting refinements that help you make expressive charts, customize and layout text, and so much more.

Migrate your TVML app to SwiftUI

Migrate your TVML app to SwiftUI

SwiftUI helps you build great apps on all Apple platforms and is the preferred toolkit for bringing your content into the living room with tvOS 18. Learn how to use SwiftUI to create familiar layouts and controls from TVMLKit, and get tips and best practices.

SwiftUI essentials

SwiftUI essentials

Join us on a tour of SwiftUI, Apple’s declarative user interface framework. Learn essential concepts for building apps in SwiftUI, like views, state variables, and layout. Discover the breadth of APIs for building fully featured experiences and crafting unique custom components. Whether you’re brand new to SwiftUI or an experienced developer, you’ll learn how to take advantage of what SwiftUI has to offer when building great apps.

Platforms State of the Union

Platforms State of the Union

Discover the newest advancements on Apple platforms.

Elevate your tab and sidebar experience in iPadOS

Elevate your tab and sidebar experience in iPadOS

iPadOS 18 introduces a new navigation system that gives people the flexibility to choose between using a tab bar or sidebar. The newly redesigned tab bar provides more space for content and other functionality. Learn how to use SwiftUI and UIKit to enable customization features – like adding, removing and reordering tabs – to enable a more personal touch in your app.