无法在所有窗口上方呈现图像

问题描述 投票:0回答:1

我有一个图像想要在所有窗口(包括 SwiftUI 中的 .sheets)上方呈现。我正在使用从下面的 SO 线程中提取的解决方案。但是,当我尝试使用该视图时,出现错误“静态方法‘buildExpression’要求‘TopImageView’符合‘View’”。如何在 SwiftUI 中的所有窗口上方正确呈现图像?

https://stackoverflow.com/a/77533196/18070009

struct ContentView: View {
    @State var showImage = false
    var body: some View {
        TopImageView(isPresented: $showImage, image: "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg")
    }
}

struct TopImageView: ViewModifier {
    @Binding var isPresented: Bool
    @State private var hostingController: UIHostingController<TopImage>? = nil
    let image: String

    func showImage() {
        let swiftUIView = TopImage(image: image)
        hostingController = UIHostingController(rootView: swiftUIView)
        hostingController?.view.backgroundColor = .clear
        hostingController?.view.frame = CGRect(
            x: 0,
            y: UIScreen.main.bounds.height,
            width: UIScreen.main.bounds.width,
            height: 0)

        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
           let window = windowScene.windows.first {
            window.addSubview(hostingController!.view)

            hostingController?.view.center.x = window.center.x

            let contentSize = hostingController!.view.sizeThatFits(CGSize(
                width: UIScreen.main.bounds.width,
                height: .greatestFiniteMagnitude))

            UIView.animate(withDuration: 0.5) {
                hostingController?.view.frame.origin.y = window.frame.height - contentSize.height
                hostingController?.view.frame.size.height = contentSize.height
            }
        }
    }

    func dismissImage() {
        hostingController?.view.removeFromSuperview()
        hostingController = nil
    }

    func body(content: Content) -> some View {
        ZStack(alignment: .bottom) {
            content
            if isPresented {
                VStack {}
                    .onAppear {
                        showImage()
                    }
                    .onChange(of: image) { newValue in
                        dismissImage()
                        showImage()
                    }
                    .onDisappear {
                        dismissImage()
                    }
            }
        }
    }
}

struct TopImage: View {
    let image: String
    @State var imageHeight: Double = 0.0
    @State var imageWidth: Double = 0.0
    @State var opac: Double = 1.0
    @State private var offset: CGPoint = .zero
    @State private var lastTranslation: CGSize = .zero

    var body: some View {
        ZStack {
            Rectangle().foregroundColor(.black).opacity(opac).ignoresSafeArea()
            GeometryReader { geometry in
                KFImage(URL(string: image))
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .offset(x: offset.x + (widthOrHeight(width: true) - imageWidth) / 2.0, y: offset.y + (widthOrHeight(width: false) - imageHeight) / 2.0)
                    .background(
                        GeometryReader { proxy in
                            Color.clear
                                .onChange(of: proxy.size.height) { _ in
                                    imageHeight = proxy.size.height
                                    imageWidth = proxy.size.width
                                }

                        }
                    )
            }
        }
        .ignoresSafeArea()
    }
}
ios swift swiftui uikit
1个回答
0
投票

您已将

TopImageView
制作为
ViewModifier
,但看起来您只是希望它成为
View
。您可以将其更改为
View
并将 body 的签名更改为
var body: some View
而不是函数

© www.soinside.com 2019 - 2024. All rights reserved.