Stefan Pauwels

Smooth Pop Transition in NavigationSplitView

In a compact environment (iPhone), navigating from content back to sidebar in a SwiftUI NavigationSplitView is causing a pop transition. The dismissed content view navigates out to the right.

If you experience lag during that transition it might be due to the way you select the content.

@State private var appSection: AppSectionEnum?

NavigationSplitView {
  SidebarView(selection: $appSection)
} content: {
  switch appSection {
    case .search:
      SearchView()
    case .articles:
      ArticlesView()
    case .images:
      ImageView()
    default:
      // ArticlesView() DON'T DO THIS
      EmptyView() // THIS IS BETTER
  }
}

Be sure to not create an expensive view for the content part, if nothing is selected (for example by creating a default article list view in a switch default-case like shown here).

On navigating back, the content view gets rebuilt because the appSection change invalidates the NavigationSplitView. So use something lightweight or even the SwiftUI standard EmptyView().

Leave a Reply

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

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.