0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

カスタムNavigationViewStyle例

Last updated at Posted at 2020-04-30
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())
38C7D426-3E71-4C7F-9247-427CDD1ED65A.jpeg
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?