【问题标题】:SwiftUI: macOS toolbar is initially greyed outSwiftUI:macOS 工具栏最初是灰色的
【发布时间】:2021-07-09 07:32:14
【问题描述】:

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

应用的首次运行:

然后鼠标悬停:

代码如下:

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 测试版中。

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

【问题讨论】:

    标签: macos swiftui toolbar


    【解决方案1】:

    错误似乎在 Button() 中。这是一个适合我的解决方法。唯一的缺点是它在按下时缺少 Button 的视觉反馈。

            .toolbar
            {
                Label("Select", systemImage: "square.and.pencil")
                    .onTapGesture(perform: {addNewItem()})
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多