Collapse a doubleColumn NavigationView detail in SwiftUI like with collapsed on UISplitViewController?

后端 未结 5 1478
渐次进展
渐次进展 2020-12-24 07:38

So when I make a list in SwiftUI, I get the master-detail split view for \"free\".

So for instance with this:

import SwiftUI

struct ContentView : Vi         


        
5条回答
  •  一个人的身影
    2020-12-24 08:05

    import SwiftUI
    
    var hostingController: UIViewController?
    
    func showList() {
        let split = hostingController?.children[0] as? UISplitViewController
        UIView.animate(withDuration: 0.3, animations: {
            split?.preferredDisplayMode = .primaryOverlay
        }) { _ in
            split?.preferredDisplayMode = .automatic
        }
    }
    
    func hideList() {
        let split = hostingController?.children[0] as? UISplitViewController
        split?.preferredDisplayMode = .primaryHidden
    }
    
    // =====
    
    struct Dest: View {
        var person: String
    
        var body: some View {
            VStack {
                Text("Hello! \(person)")
                Button(action: showList) {
                    Image(systemName: "sidebar.left")
                }
            }
            .onAppear(perform: hideList)
        }
    }
    
    struct ContentView : View {
        var people = ["Angela", "Juan", "Yeji"]
    
        var body: some View {
            NavigationView {
                List {
                    ForEach(people, id: \.self) { person in
                        NavigationLink(destination: Dest(person: person)) {
                            Text(person)
                        }
                    }
                }
                VStack {
                    Text("

提交回复
热议问题