import SwiftUI
public struct AllVisibleNavigationViewStyle: NavigationViewStyle {
struct ControllerModifier: ViewModifier {
struct ControllerView: UIViewControllerRepresentable {
class ViewController: UIViewController {
var perform: (UIViewController) -> Void = { _ in }
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
perform(self)
}
}
var perform: (UIViewController) -> Void
func makeUIViewController(context: Self.Context) -> UIViewController {
let vc = ViewController()
vc.perform = perform
return vc
}
func updateUIViewController(_ uiViewController: UIViewController, context: Self.Context) {}
}
var perform: (UIViewController) -> Void
func body(content: Content) -> some View {
content.overlay (
ControllerView(perform: perform).frame(width: 0, height: 0)
)
}
}
public func _body(configuration: _NavigationViewStyleConfiguration) -> some View {
NavigationView { configuration.content }
.modifier(ControllerModifier(perform: { vc in
guard let svc = vc.parent?.children.first as? UISplitViewController else { return }
svc.preferredDisplayMode = .allVisible
}))
}
public init(){}
}
struct ContentView: View {
struct Greet: View {
var text: String
var body: some View {
Text(text).navigationBarTitle("Secondary")
}
}
var body: some View {
NavigationView {
List(0..<100) { i in
NavigationLink(destination: Greet(text: "hello \(i)")) {
Text("greeting \(i)")
}
}.navigationBarTitle("Primary")
}.navigationViewStyle(AllVisibleNavigationViewStyle())
}
}
import PlaygroundSupport
PlaygroundPage.current.wantsFullScreenLiveView = false
PlaygroundPage.current.setLiveView(ContentView())