admin管理员组文章数量:1333396
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
1 Answer
Reset to default 0you could try using a HStack
, such as
Section(header:
HStack{
Text("Favorites")
Spacer()
Button(action: { addNewItem() }) {
Image(systemName: "plus.circle")
}.buttonStyle(.plain)
})
....
本文标签: swifthow to add plus button to list section in the sidebarStack Overflow
版权声明:本文标题:swift - how to add plus button to list section in the sidebar - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742324681a2453486.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论