本文介绍了SwiftUI:macOS 工具栏最初是灰色的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我第一次启动我的应用程序时,工具栏图标会显示并且工作正常,但它们最初是灰色的,直到将鼠标悬停在按钮上.

When I first launch my app the toolbar icons are shown and work fine, but they are initially greyed out until mousing over the button.

首次运行应用程序:

然后将鼠标悬停:

代码如下:

import SwiftUI

struct ContentView: View
{
    @State var mainList:[String] = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "Indigo", "Juliet", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whisky", "Xray", "Yankee", "Zulu"]
    @State var selectedItem:String? = "Echo"
    var body: some View
    {
        NavigationView
        {
            List()
            {
                ForEach(mainList, id: \.self)
                {item in
                    NavigationLink(destination: DetailView(item: item), tag: item, selection: $selectedItem, label:
                                    {
                                        Text("\(item)")
                                    })
                    
                }
            }
        }
        .toolbar
        {
            Button(action: {addNewItem()})
            {
                Label("Select", systemImage: "square.and.pencil")
            }
        }
        
    }
    
    func addNewItem()
    {
        print ("Add item")
    }
    
}

struct DetailView: View
{
    @State var item: String
    
    var body: some View
    {
        Text(item)
    }
}

这是在 Xcode 12.5.1 和 Xcode 13.0 测试版中.

This is in Xcode 12.5.1 and Xcode 13.0 beta.

我本以为应该使用此代码在活动时启用工具栏.是错误还是我做错了什么?

I would have thought that the toolbar should be enabled at active with this code. Is it a bug or am I doing something wrong?

推荐答案

使用 ToolbarItem.

        .toolbar{
            ToolbarItem(placement: .primaryAction) {
                Button(action: {addNewItem()}){
                    Label("Select", systemImage: "square.and.pencil")
                }
            }
        }

https://developer.apple.com/documentation/swiftui/toolbaritem

这篇关于SwiftUI:macOS 工具栏最初是灰色的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 06:52