swift - how to add plus button to list section in the sidebar - Stack Overflow

How do I show a plus button at the section header of the list view? The screenshot attached is the Mail

How do I show a plus button at the section header of the list view? The screenshot attached is the Mail app from MacOS. E.g. the code below adds button at the top, which I dont want, but would like it for each section.

import SwiftUI

struct ContentView: View {
    @State private var items: [String] = ["Inbox", "Sent"]
    @State private var selectedItem: String? = "Inbox"
    
    var body: some View {
        NavigationSplitView {
            List(selection: $selectedItem) {
                Section(header: Text("Favorites")) {
                    ForEach(items, id: \.self) { item in
                        Label(item, systemImage: item == "Inbox" ? "tray" : "paperplane")
                            .tag(item)
                    }
                }
            }
            .toolbar {
                ToolbarItem(placement: .automatic) {
                    Button(action: {
                        // Add action for the "+" button
                        addNewItem()
                    }) {
                        Image(systemName: "plus")
                    }
                }
            }
            .listStyle(SidebarListStyle()) // Sidebar appearance
        } detail: {
            if let selectedItem = selectedItem {
                Text("\(selectedItem) Details")
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
            } else {
                Text("Select an item")
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
            }
        }
    }
    
    private func addNewItem() {
        // Example action for adding a new item
        items.append("New Item \(items.count + 1)")
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

How do I show a plus button at the section header of the list view? The screenshot attached is the Mail app from MacOS. E.g. the code below adds button at the top, which I dont want, but would like it for each section.

import SwiftUI

struct ContentView: View {
    @State private var items: [String] = ["Inbox", "Sent"]
    @State private var selectedItem: String? = "Inbox"
    
    var body: some View {
        NavigationSplitView {
            List(selection: $selectedItem) {
                Section(header: Text("Favorites")) {
                    ForEach(items, id: \.self) { item in
                        Label(item, systemImage: item == "Inbox" ? "tray" : "paperplane")
                            .tag(item)
                    }
                }
            }
            .toolbar {
                ToolbarItem(placement: .automatic) {
                    Button(action: {
                        // Add action for the "+" button
                        addNewItem()
                    }) {
                        Image(systemName: "plus")
                    }
                }
            }
            .listStyle(SidebarListStyle()) // Sidebar appearance
        } detail: {
            if let selectedItem = selectedItem {
                Text("\(selectedItem) Details")
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
            } else {
                Text("Select an item")
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
            }
        }
    }
    
    private func addNewItem() {
        // Example action for adding a new item
        items.append("New Item \(items.count + 1)")
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Share Improve this question asked Nov 20, 2024 at 22:29 PranavPranav 5711 gold badge9 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

you could try using a HStack, such as

Section(header:
    HStack{
      Text("Favorites")
      Spacer()
      Button(action: { addNewItem() }) {
          Image(systemName: "plus.circle")
      }.buttonStyle(.plain)
    })
  ....

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742324682a4422509.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信