0

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?

  • Is there a reason you're using `EmptyView` like this? Why not, for example, since you're already setting a background, just use `Color.green`? – jnpdx Sep 28 '21 at 01:45
  • Your code would be never working! even with iOS 13.0 or 14.0 it is impossible! EmptyView means nothing for SwiftUI! It is like multiplying zero to one million! what would be result? – ios coder Sep 28 '21 at 01:50
  • 1
    No, It's works in iOS 13, 14. My project use too many Empty View. I can't change another.... – 윤시완 Sep 28 '21 at 04:59
  • I am also getting the same issue with empty view if you able to resolve it please provide the solution, in my code I am using empty view to present the full screen as there are multiple conditions so empty view is needed we directly not able to use .overfullscreen – VSP Sep 29 '21 at 07:09
  • An answer was provided below, hopefully it solves the issue. – cole Sep 30 '21 at 06:25

3 Answers3

0

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

cole
  • 1,039
  • 1
  • 8
  • 35
  • Usually you need several EmptyViews for different sheets. Your case works only for one sheet if you will add another one it will not work. And right now from iOS 15 and new you can apply several sheets to one view. Finally! – Mr.OFF Sep 30 '21 at 09:58
0

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())
  })
Mr.OFF
  • 108
  • 5
0

Replace "EmptyView()" with "Spacer().frame(height:0)" did work for me on iOS15 and iOS14

  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Jan 10 '22 at 17:21