Skip to content

Incorrect sort order when using NavigationStack #168

@DavidBrunow

Description

@DavidBrunow

I've found that when using a NavigationStack in a SwiftUI view accessibility snapshot puts the contents of the navigation bar below the contents of the view that is contained in the NavigationStack. That is a lot of words that may or may not be helpful so I'll share some snapshots:

NavigationView NavigationStack
testNavigationView 1 testNavigationStack 1

Here is the code I used to generate those snapshots:

import AccessibilitySnapshot
import SnapshotTesting
import SwiftUI
import XCTest

class NavigationExampleSnapshotTests: XCTestCase {
  func testNavigationView() {
    let view = NavigationView {
      Text("Text inside a NavigationView")
        .navigationTitle("Navigation View")
        .toolbar {
          ToolbarItem {
            Button {
              // no-op
            } label: {
              Text("Add")
            }
          }
        }
    }

    let hostingController = UIHostingController(rootView: view)
    hostingController.view.frame = UIScreen.main.bounds

    assertSnapshot(
      of: hostingController,
      as: .accessibilityImage(drawHierarchyInKeyWindow: true)
    )
  }

  func testNavigationStack() {
    let view = NavigationStack {
      Text("Text inside a NavigationStack")
        .navigationTitle("Navigation Stack")
        .toolbar {
          ToolbarItem {
            Button {
              // no-op
            } label: {
              Text("Add")
            }
          }
        }
    }

    let hostingController = UIHostingController(rootView: view)
    hostingController.view.frame = UIScreen.main.bounds

    assertSnapshot(
      of: hostingController,
      as: .accessibilityImage(drawHierarchyInKeyWindow: true)
    )
  }
}

I've noticed this issue when running Xcode 15 in an iOS 17 simulator. I have not tested previous simulators but I think the issue is related to NavigationStack and not to the simulator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1: High PriorityIssues that would result in incorrectly accessing whether or not a view is accessible.Switch Control parityMismatch between output of framework and actual Switch Control behaviorVoiceOver parityMismatch between output of framework and actual VoiceOver behaviorbugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions