skip to Main Content

I have serious problem. My Xcode version is 13, iOS version is 15.

import SwiftUI

struct ContentView: View {
  @State var isGo: Bool = false

  var body: some View {
    ZStack {
      Button(action: {
        self.isGo = true
      }, label: {
        Text("Go EmptyView")
      })

      EmptyView()
        .background(Color.green)
        .frame(width: 100, height: 100)
        .sheet(isPresented: $isGo, onDismiss: nil, content: {
          PopupView()
        })
    }
  }
}

struct PopupView: View {
    var body: some View {
        Rectangle()
            .fill(Color.green)
    }
}

Above code is not working. But, Previous Xcode version or Previous iOS version is that code is working. Is that iOS bug? Is there anything solution?

3

Answers


  1. You’re very close, not sure why nobody has offered help. Your code does work but in theory isn’t correct, you needed to move the where you called .sheet to outside your ZStack and it works. But here’s a better approach without all the useless code.

    struct ContentView: View {
        @State var showemptyview: Bool = false
        
        var body: some View {
            Button("Go EmptyView") {
                showemptyview.toggle()
            }
            .sheet(isPresented: $showemptyview) {
                EmptyView()
                    .background(Color.green)
            }
        }
    }
    

    enter image description here

    Login or Signup to reply.
  2. Finally in a last version you can use several sheets directly for main view and it works. You don’t need to create separate EmptyView() with sheet

    YourMainView()
      .sheet(item: $viewModel) { item in
        // some logic here 
      }
      .sheet(isPresented: $onther_viewModel.showView, content: {
        SomeAnotherView(viewModel: viewModel.getVM())
      })
      .sheet(isPresented: $onther_viewModel2.showView, content: {
        SomeView(viewModel: viewModel.getVM())
      })
    
    Login or Signup to reply.
  3. Replace "EmptyView()" with "Spacer().frame(height:0)" did work for me on iOS15 and iOS14

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search