I’ve a picture that I need to current above all home windows together with .sheets in SwiftUI. I’m utilizing the answer pulled from the SO thread beneath. Nonetheless when I attempt to use the view I get the error “Static methodology ‘buildExpression’ requires that ‘TopImageView’ conform to ‘View'”. How can I accurately current an Picture above all home windows in SwiftUI?
https://stackoverflow.com/a/77533196/18070009
struct ContentView: View {
@State var showImage = false
var physique: some View {
TopImageView(isPresented: $showImage, picture: "https://cdn.pixabay.com/photograph/2015/04/23/22/00/tree-736885_1280.jpg")
}
}
struct TopImageView: ViewModifier {
@Binding var isPresented: Bool
@State personal var hostingController: UIHostingController<TopImage>? = nil
let picture: String
func showImage() {
let swiftUIView = TopImage(picture: picture)
hostingController = UIHostingController(rootView: swiftUIView)
hostingController?.view.backgroundColor = .clear
hostingController?.view.body = CGRect(
x: 0,
y: UIScreen.essential.bounds.top,
width: UIScreen.essential.bounds.width,
top: 0)
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let window = windowScene.home windows.first {
window.addSubview(hostingController!.view)
hostingController?.view.heart.x = window.heart.x
let contentSize = hostingController!.view.sizeThatFits(CGSize(
width: UIScreen.essential.bounds.width,
top: .greatestFiniteMagnitude))
UIView.animate(withDuration: 0.5) {
hostingController?.view.body.origin.y = window.body.top - contentSize.top
hostingController?.view.body.dimension.top = contentSize.top
}
}
}
func dismissImage() {
hostingController?.view.removeFromSuperview()
hostingController = nil
}
func physique(content material: Content material) -> some View {
ZStack(alignment: .backside) {
content material
if isPresented {
VStack {}
.onAppear {
showImage()
}
.onChange(of: picture) { newValue in
dismissImage()
showImage()
}
.onDisappear {
dismissImage()
}
}
}
}
}
struct TopImage: View {
let picture: String
@State var imageHeight: Double = 0.0
@State var imageWidth: Double = 0.0
@State var opac: Double = 1.0
@State personal var offset: CGPoint = .zero
@State personal var lastTranslation: CGSize = .zero
var physique: some View {
ZStack {
Rectangle().foregroundColor(.black).opacity(opac).ignoresSafeArea()
GeometryReader { geometry in
KFImage(URL(string: picture))
.resizable()
.aspectRatio(contentMode: .match)
.offset(x: offset.x + (widthOrHeight(width: true) - imageWidth) / 2.0, y: offset.y + (widthOrHeight(width: false) - imageHeight) / 2.0)
.background(
GeometryReader { proxy in
Colour.clear
.onChange(of: proxy.dimension.top) { _ in
imageHeight = proxy.dimension.top
imageWidth = proxy.dimension.width
}
}
)
}
}
.ignoresSafeArea()
}
}