我在哪里可以找到 SwiftUI 中的微调图标?

bap*_*482 3 macos swift swiftui

我了解到可以像这样渲染系统图像:

Image(nsImage: NSImage(imageLiteralResourceName: NSImage.addTemplateName))
Run Code Online (Sandbox Code Playgroud)

但是似乎没有对应的 spinner 模板名称: 在此处输入图片说明

我必须使用自定义 svg 吗?

Moj*_*ini 8

Xcode 12

这只是一个简单的看法。

ProgressView()
Run Code Online (Sandbox Code Playgroud)

目前,它是默认的,CircularProgressViewStyle但您可以通过添加以下修饰符来手动设置它的样式:

.progressViewStyle(CircularProgressViewStyle())
Run Code Online (Sandbox Code Playgroud)

此外,样式可以是任何符合 ProgressViewStyle


Xcode 11

您可以NSProgressIndicator直接在 SwiftUI 中使用:

执行

struct ProgressIndicator: NSViewRepresentable {
    
    typealias TheNSView = NSProgressIndicator
    var configuration = { (view: TheNSView) in }
    
    func makeNSView(context: NSViewRepresentableContext<ProgressIndicator>) -> NSProgressIndicator {
        TheNSView()
    }
    
    func updateNSView(_ nsView: NSProgressIndicator, context: NSViewRepresentableContext<ProgressIndicator>) {
        configuration(nsView)
    }
}
Run Code Online (Sandbox Code Playgroud)

示例用法

var body: some View {
    ProgressIndicator {
        $0.controlTint = .blueControlTint
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以替换TheNSViewNSView您需要在 SwiftUI 中使用的任何其他内容。

  • 为了跟进这个答案,这里有一个来自开源项目的围绕“NSProgressIndicator”的 SwiftUI 包装示例:https://github.com/SwiftUIX/SwiftUIX/blob/73ee6bfb44e76ae08a146e7e575176743f0615e9/Sources/Intramodular/Progress/ActivityIndi​​cator.swift# L56-L75 (2认同)