Open f3dm76 opened 2 months ago
And that is if I attach it further down the hierarchy - seems like it always replaces something with black
Try giving the View you want to protect a background, that seems to work for me example:
import SwiftUI
import ScreenShield
struct ContentView: View {
@State var textHeight: CGFloat = 10
var body: some View {
VStack {
Spacer()
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
ProtectedView {
HStack {
Text("Hello, world!")
}
.background(.red)
}
Spacer()
}
.background(.gray)
.padding()
}
}
#Preview {
ContentView()
}
struct ProtectedView<Content: View>: View {
@ViewBuilder let content: () -> Content
@State var contentDimensions: CGSize? = nil
var body: some View {
content()
.background() {
GeometryReader { geometry in
Spacer()
.onChange(of: geometry.size.height) {
contentDimensions = geometry.size
}
}
}
.protectScreenshot()
.frame(maxWidth: contentDimensions?.width, maxHeight: contentDimensions?.height)
}
}
Attaching .protectScreenshot() to root view adds black lines
with .protectScreenshot()
without .protectScreenshot()