Home IOS Development ios – NavigationStack with path does not work correctly inside UIHostingController

ios – NavigationStack with path does not work correctly inside UIHostingController

0
ios – NavigationStack with path does not work correctly inside UIHostingController

[ad_1]

When UIHostingController‘s rootView is wrapped in a NavigationStack, navigation by way of NavigationStack‘s path does not work: Despite the fact that a price is appended to the NavigationPath, the corresponding view isn’t pushed.

Curiously sufficient, if NavigationStack is initialized with a non-empty path, it reveals the stack accurately:

(On the animation, the display with the “Present first” button is a UIViewController, which modally presents a SwiftUI view wrapped in a UIHostingController (the view with the button “Open subsequent”.) “Open subsequent” modifies path of the view, attempting to push one other SwiftUI view, with the “Subsequent” textual content label.

This can be a minimal reproducible instance, stripped of all irrelevant code:

class HomeViewController: UIViewController {

  class NavigationModel: ObservableObject {

    enum Path {
      case second
    }

    @Printed var navigationPath = NavigationPath()

  }

  @ObservedObject non-public var navigationModel = NavigationModel()

  override func viewDidLoad() {
    tremendous.viewDidLoad()

    view.backgroundColor = .white

    let button = UIButton(sort: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Present first", for: .regular)
    button.addTarget(self, motion: #selector(presentSecondViewController), for: .touchUpInside)

    view.addSubview(button)
    NSLayoutConstraint.activate([button.centerXAnchor.constraint(equalTo: view.centerXAnchor), button.centerYAnchor.constraint(equalTo: view.centerYAnchor)])
  }

  @objc func presentSecondViewController() {
    let view = NavigationStack(path: $navigationModel.navigationPath) {
      Button("Open subsequent") { self.navigationModel.navigationPath.append(NavigationModel.Path.second) }
        .navigationDestination(for: NavigationModel.Path.self) {
          change $0 {
            case .second:
              SecondView()
          }
        }
    }

    let vc = UIHostingController(rootView: view)
    current(vc, animated: true)
  }

}

struct SecondView: View {
  var physique: some View {
    Textual content("Subsequent")
  }
}

Nonetheless, the identical strategy works within the pure SwiftUI: If the foundation view modally presents one other view wrapped in a NavigationStack, all modifications of the corresponding path consequence within the anticipated habits:

Correct behavior of NavigationStack in pure SwiftUI

Here is the code of the working instance:

@essential struct SwiftUIPlaygroundApp: App {

  class NavigationModel: ObservableObject {

    enum Path {
      case second
    }

    @Printed var navigationPath = NavigationPath()

  }

  var physique: some Scene {
    WindowGroup {
      Button("Present first") { firstShown = true }
        .sheet(isPresented: $firstShown) {
          NavigationStack(path: $navigationModel.navigationPath) {
            Button("Open subsequent") { navigationModel.navigationPath.append(NavigationModel.Path.second) }
              .navigationDestination(for: NavigationModel.Path.self) {
                change $0 {
                  case .second:
                    SecondView()
                }
              }
          }
        }
    }
  }
  @State var firstShown = false
  @ObservedObject var navigationModel = NavigationModel()

}

struct SecondView: View {
  var physique: some View {
    Textual content("Subsequent")
  }
}

How may I make it work correctly inside UIHostingController?

Any steerage is appreciated.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here