首页 > 解决方案 > Swift ui macos ProgressView 使用未解析的标识符'ProgressView'

问题描述

在此处输入图像描述

我正在尝试在 macOS 项目中添加 ProgressView,但它给了我以下错误,在互联网上查找我找不到任何解决方案。

谁能帮我吗?

标签: swiftmacosswiftuiloader

解决方案


此 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

推荐阅读