0

我想使用 NSViewRepresentable 在 SwiftUI 中创建一个模糊视图,但 XCode 给了我一条错误消息,上面写着“在范围内找不到类型 'NSViewRepresentable'”。

import SwiftUI

struct BlurWindow: NSViewRepresentable { //Cannot find type 'NSViewRepresentable' in scope

    func makeNSView(context: Context) -> NSVisualEffectView { //Cannot find type 'Context' in scope

        let view = NSVisualEffectView() //Cannot find type 'NSVisualEffectView' in scope
        view.blendingMode = .behindWindow
        return view
    }

    func updateNSView(_ nsView: NSVisualEffectView, context: Context) { //Cannot find type 'Context' in scope

    }
}

但是,当我将这个完全相同的代码复制并粘贴到一个新的全新 Xcode 项目中时,没有错误。我在 Google 和 StackOverflow 上进行了搜索,但没有发现任何人有同样的问题。

4

1 回答 1

1

你的项目有 iOS 目标吗?你在建立那个目标吗?如果是这样,请确保该文件不包含在此目标中(使用 Target Membership 下的文件检查器),因为 NSViewRepresentable 在 iOS 上不可用。

或者,您可以将代码包装在#if os(macOS)和中#endif

于 2021-01-11T14:57:36.600 回答