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 *

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)