0

I have the problem that my sheet is not working as it should. When using a second NavigationView inside the SecondView then the sheet works but it looks crazy (first screenshot). When I comment out the NavigationView in SecondView the app looks correct (second screenshot), but the sheet is only working once when I open it from the Add button in navigation bar. When I use the "open sheet"-button in SecondView then the sheet works correct.

Did I do something wrong with this:

.navigationBarItems(trailing: Button("Add") {
            showSheet.toggle()
        })

SecondView with NavigationView:

SecondView with NavigationView

SecondView without NavigationView (how it should look like):

SecondView without NavigationView

This is the code I'm using:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("Start View")
                    .font(.title)
                NavigationLink(destination: SecondView()) {
                    CustomButton()
                }
            }
        }
    }
}

struct CustomButton: View {
    var body: some View {
        HStack {
            Spacer()
            Text("Go To Second View")
                .font(.headline)
                .foregroundColor(.red)
                .padding()
            Spacer()
        }
        .background(Color.white)
        .overlay(
            RoundedRectangle(cornerRadius: 20)
                .stroke(Color.red, lineWidth: 2)
        )
        .padding()
    }
}

struct SecondView: View {
    @State private var showSheet = false
    var body: some View {
        //NavigationView{
        Text("Second View")
        Button("open sheet", action: {
            showSheet.toggle()
        })
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(trailing: Button("Add") {
            showSheet.toggle()
        })
        .sheet(isPresented: $showSheet, content: {
            SecondViewsSheet()
        })
        //}
    }
}

struct SecondViewsSheet: View {
    
    var body: some View {
        Text("Sheet")
            .navigationBarItems(trailing: Button("Test"){
            })
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I use Xcode 12.2 with an iPhone 12 Pro simulator.

m8xp0w3r
  • 85
  • 1
  • 8

0 Answers0