Swift UI MacOS ProgressView使用未解析的标识符'ProgressView'

问题描述

enter image description here

我试图在macOS项目中添加一个ProgressView,但是它给我以下错误,在Internet上查找找不到任何解决方案。

有人可以帮我吗?

解决方法

此 ProgressView 仅适用于 MacOs 11,您可以在此链接中看到 https://developer.apple.com/documentation/swiftui/progressview

您可以使用 NSProgressIndicator 并将其包装成 NSViewRepresentable,如下所示:

import Swift
import SwiftUI

public struct ActivityIndicator {
    public enum Style {
        case medium
        case large
    }
    
    private var isAnimated: Bool = true
    private var style: Style? = Style.medium
    
    public init() {
        
    }
}

#if os(macOS)

import Cocoa
import AppKit

extension ActivityIndicator: NSViewRepresentable {
    public typealias Context = NSViewRepresentableContext<Self>
    public typealias NSViewType = NSProgressIndicator
    
    public func makeNSView(context: Context) -> NSViewType {
        let nsView = NSProgressIndicator()
        nsView.isIndeterminate = true
        nsView.style = .spinning
        nsView.sizeToFit()
        nsView.layer?.transform = CATransform3DMakeScale(1.0,0.6,0.0);
        nsView.controlSize = .small
        return nsView
    }
    
    public func updateNSView(_ nsView: NSViewType,context: Context) {
        isAnimated ? nsView.startAnimation(self) : nsView.stopAnimation(self)
    }
}

#endif