0

Scenario:

I have a screen with an NavigationStack, after going to the second screen, The back button of the NavigationStack appears. In that screen, by clicking the button "Toggle View", a fake modal appears with an opacity animation. Everything ok until now, but the problem is that the back button of the navigationStack is not hiddden by the background of the modal.

How can I fix this?

import SwiftUI

struct ContentView: View {
  @State private var isViewAVisible = false

  var body: some View {
    NavigationStack {
      VStack {
        NavigationLink("Show Detail View") {
          SecondView()
        }
      }
      .navigationTitle("Navigation")
    }
  }
}

struct SecondView: View {
  @State private var isViewAVisible = false

  var body: some View {
    NavigationStack {
      ZStack {
        Color.red.ignoresSafeArea()
        VStack {
          Spacer()
          Button("Toggle View") {
            isViewAVisible.toggle()

          }
        }

        //logic to present the modal
        ZStack {
          if isViewAVisible {
            Color.black.opacity(0.6).ignoresSafeArea()
            VStack {
              Button {
                isViewAVisible.toggle()
              } label: {
                Text("Hello")
              }
              .frame(height: 300)
              Button {} label: {
                Text("Hello 2 ")
              }
              .frame(height: 300)
            }
          }
        }
        .animation(.easeIn, value: isViewAVisible)
      }
    }
  }
}

The screenshot of how it looks like:

enter image description here

It should like like this:

enter image description here

This solution is using fullScreenCover modifier and that fix the issue, but now I cannot present the modal with an opacity animation.

Also if someone has idea on how to do it this way would be great!

Here the code for that view:

'''

struct SecondView: View {
  @State private var isViewAVisible = false

  var body: some View {
    NavigationStack {
      ZStack {
        Color.red.ignoresSafeArea()
        VStack {
          Spacer()
          Button("Toggle View") {
            isViewAVisible.toggle()

          }
        }


      }
      .fullScreenCover(isPresented: $isViewAVisible) {
        ZStack{
          VStack {
            Button {
              isViewAVisible.toggle()
            } label: {
              Text("Hello")
            }
            .frame(height: 300)
            Button {} label: {
              Text("Hello 2 ")
            }
            .frame(height: 300)
          }
        }
        .presentationBackground(Color.black.opacity(0.6))
      }
    }
  }
}

'''

CarlosAndres
  • 585
  • 1
  • 5
  • 14
  • 1
    You can hide the back button when the modal is presented using `.navigationBarBackButtonHidden()`. How about that instead? – Sweeper Jul 23 '23 at 03:12
  • That kind of works, but the animation when hidding the back button is making look weird the transition. Esentially, the only animation should be the modal appearing, and the back button still visible below this modal. @Sweeper – CarlosAndres Jul 23 '23 at 19:06
  • @Sweeper I added an image of the expected result. Notice how the back button is still visible, but it's below the modal. Also I added the code for the fullScreenCover solution – CarlosAndres Jul 23 '23 at 19:10

0 Answers0