I don't want black background in presented sheet

I want a different color, one from my asset catalog, as the background of my first ever swift UI view (and, well, swift, the rest of the app is still obj.c)

I've tried putting the color everywhere, but it does't take. I tried with just .red, too to make sure it wasn't me. Does anyone know where I can put a color call that will actually run? Black looks very out of place in my happy app. I spent a lot of time making a custom dark palette. TIA KT

    @State private var viewModel = ViewModel()
    @State private var showAddSheet = false
    
    var body: some View {
        ZStack {
            Color.myCuteBg
                .ignoresSafeArea(.all)
            
            NavigationStack {
                content
                    .navigationBarTitleDisplayMode(.inline)
                    .toolbar {
                        ToolbarItem(placement: .principal) {
                            Image("cute.image")
                                .font(.system(size: 30))
                                .foregroundColor(.beigeTitle)
                        }
                    }
            }
            .background(Color.myCuteBg)
            .presentationBackground(.myCuteBg)
            .sheet(isPresented: $showAddSheet) {
                AddView()
            }
            .environment(viewModel)
            .onAppear {
                viewModel.fetchStuff()
            }
        }
        .tint(.cuteColor)
    }
    
    @ViewBuilder var content: some View {
        if viewModel.list.isEmpty && viewModel.anotherlist.isEmpty {
            ContentUnavailableView(
                "No Content",
                image: "stop",
                description: Text("Add something here by tapping the + button.")
            )
        } else {
            contentList
        }
    }
    
     var contentList: some View {
        blah blah blah
    }
}

First I tried the background, then the presentation background, and finally the Zstack. I hope this is fixed because it's actually fun to build scrollable content and text with swiftUI and I'd been avoiding it for years.

Answered by DTS Engineer in 847846022

Adding a background to your view covers how to add a background behind your view.

Apply the learnings from the article in a small test project to test and verify. If you're still getting the same black screen, please share a link to your test project. That'll help us better understand what's going on. If you're not familiar with preparing a test project, take a look at Creating a test project.

.sheet(isPresented: $showAddSheet) {
  AddView()
    .presentationBackground(yourColor)
}

Oh thanks for the reply. I meant that the content view is. being presented as a sheet itself. I tried the code above, and the child also has a black glass sheet.

So, maybe pretend I don't present anything.

Accepted Answer

Adding a background to your view covers how to add a background behind your view.

Apply the learnings from the article in a small test project to test and verify. If you're still getting the same black screen, please share a link to your test project. That'll help us better understand what's going on. If you're not familiar with preparing a test project, take a look at Creating a test project.

Thanks. I've added the test project to my GitHub here. It's an objective c storyboard hosting sample code from this year's WWDC. (which btw would be nice if this sample were swift 6 compatible so the compiler doesn't complain.)

I've managed to make the background this lovely gradient, thanks for the link. My mistake was in putting the background on the body, not inside the Navigation stack. Clearly I'm used to Storyboards. But what I really want is to have it show up as a glass sheet when I present it from my objective C app, but it's not doing that, either. I tried making the background clear, but it goes back to showing up as black. How do I make it show up as glass?

I accepted the answer since it was to my original question, of course now I still have an open question about presenting that hosting controller in a way so that it will show up as a glass sheet.

For anyone following along at home. I'm able to present SwiftUI controllers as glass in my objective-c storyboard app if I recursively keep setting subviews backgrounds to clear in my swift hosting view controller subclass. The offender with a background is "UIHostingView" I could turn it off by name but that's probably a bad idea. Maybe a 'hostsGlass' property on UIHostingController ? FB19403683

I don't want black background in presented sheet
 
 
Q