zumzum
zumzum

Reputation: 20198

SwiftUI: two column NavigationView not loading details after rotation?

I have the following SwiftUI code:

struct ContentView: View {
    var body: some View {
        NavigationView {
            Form {
                Section {
                    NavigationLink {
                        DetailsView()
                    } label: {
                        Text("Show details")
                    }
                }
            }
            Text("Select details")
        }
    }
}


struct DetailsView: View {
    
    @State var showModal = false
    
    var body: some View {
        ZStack {
            Color.gray.ignoresSafeArea()
            VStack {
                Spacer()
                Button {
                    showModal.toggle()
                } label: {
                    Text("Show modal")
                        .foregroundColor(.black)
                        .font(.system(size: 20, weight: .bold))
                }
            }
        }
        .fullScreenCover(isPresented: $showModal) {
            MyModalView {
                showModal = false
            }
        }
    }
}


struct MyModalView: View {
    
    
    var someAction:()->()
    
    var body: some View {
        ZStack {
            Color.black.ignoresSafeArea()
            Button {
                someAction()
            } label: {
                Text("some action")
                    .foregroundColor(.white)
            }
        }
    }
    
}

I am experiencing the following bug, where tapping on "Show details" won't show the DetailsView anymore after rotation...

enter image description here

How can I fix this?

Upvotes: 0

Views: 60

Answers (1)

AdR
AdR

Reputation: 882

Navigation view is really problematical but will be improved with IOS 16. Here is my solution

struct ContentView: View {
@State var navigate = false
var body: some View {
    NavigationView {
        Form {
            Section {
                NavigationLink(destination: DetailsView(navigate: $navigate), isActive: $navigate){
                    Text("Show details")
                }
            }
        }
        Text("Select details")
    }
    .navigationViewStyle(.automatic)
}
}


struct DetailsView: View {

@Binding var navigate: Bool
@State var showModal = false

var body: some View {
    ZStack {
        Color.gray.ignoresSafeArea()
        VStack {
            Spacer()
            Button {
                showModal.toggle()
            } label: {
                Text("Show modal")
                    .foregroundColor(.black)
                    .font(.system(size: 20, weight: .bold))
            }
        }
    }
    .fullScreenCover(isPresented: $showModal) {
        MyModalView {
            showModal = false
        }
    }
    .onAppear(){
        navigate = false
    }
}}


struct MyModalView: View {
var someAction:()->()
var body: some View {
    ZStack {
        Color.black.ignoresSafeArea()
        Button {
            someAction()
        } label: {
            Text("some action")
                .foregroundColor(.white)
        }
    }
}}

Upvotes: 1

Related Questions