我有一个图像,我想要显示在所有窗口之上,包括SwiftUI中的.Sheet.我正在使用从下面的so帖子中提取的解决方案.然而,当我try 使用该视图时,我得到错误"静态方法‘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()
    }
}

推荐答案

你已经得到了TopImageView分,但看起来你只想要View分.您可以将其更改为View,并将Body的签名更改为var body: some View,而不是函数

Ios相关问答推荐

缺少预期的键:';NSPrival yCollectedDataTypes';

IFrame内容拒绝仅在iOS浏览器上加载-即使发布了内容-安全-策略(CSP)框架-祖先指令

验证开发人员应用程序证书是否已在您的设备上验证

为什么applicationState发布者只发布一次?

如何在SwiftUI中实现淡入淡出加粘标题屏幕效果

比较 `某个协议` 实例时出现编译错误

更改图标会导致 SwiftUI 动画出现故障?

swiftUI中的剪辑形状无法使用旋转效果

SwiftUI:如何用拇指移动滑块值

如何让我的 iOS 应用程序与 api 连接?

如何将私钥添加到分发证书?

UICollectionView - 水平滚动,水平布局?

检测 iPhone/iPad/iPod touch 的 colored颜色 ?

动画 UILabel 字体大小更改

导航控制器自定义过渡动画

iOS - 确保在主线程上执行

判断可选数组是否为空

用户拒绝使用后如何提示用户打开位置服务

判断密钥是否存在于 NSDictionary 中

NSURL 到带有 XCTest 的测试包中的文件路径