Wie ignoriere ich SafeArea, wenn ich Inhalte in SwiftUI Sheet präsentiere?IOS

Programmierung für iOS
Anonymous
 Wie ignoriere ich SafeArea, wenn ich Inhalte in SwiftUI Sheet präsentiere?

Post by Anonymous »

Ich präsentiere eine SwiftUI-Ansicht mit einem UIHostingController mit modalPresentationStyle = .overCurrentContext. In dieser SwiftUI-Stammansicht präsentiere ich ein .sheet mit einem benutzerdefinierten Modifikator, um den Blatthintergrund transparent zu machen.
Minimal reproduzierbarer Code:

Code: Select all

import SwiftUI

class SheetViewModel: ObservableObject {
@Published var isPresented: Bool = true
}

struct RootSheetWrapper: View {
@StateObject var viewModel: SheetViewModel

var body: some View {
// Transparent host view acting as the anchor for the sheet
Color.clear
.customTransparentSheet(isPresented: $viewModel.isPresented, height: 400) {
SheetContent()
}
}
}

// Minimal version of my custom sheet extension
extension View {
func customTransparentSheet(
isPresented: Binding,
height: CGFloat,
@ViewBuilder content: @escaping () -> Content
) -> some View {
ZStack {
self
SomeOverlay()
}
self.sheet(isPresented: isPresented) {
content()
.presentationDragIndicator(.hidden)
.applyTransparentBackground()
.presentationDetents([.height(height)])
}
}
}

private extension View {
@ViewBuilder
func applyTransparentBackground() -> some View {
if #available(iOS 16.4, *) {
self.presentationBackground(.clear)
} else {
self.background(Color.clear.ignoresSafeArea())
}
}
}
Blattinhalt:

Code: Select all

struct SheetContent: View {
var body: some View {
VStack(spacing: 0) {
// This image should sit behind the home indicator
Image(systemName: "star.fill")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxWidth: .infinity)
}
// This makes the background transparent, but the bottom safe area
// still clips the Red background/Image above.
// .ignoresSafeArea(.all) //

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post