如何使用SwiftUI在macOS中制作出类似于Xcode里那种左右toolbar栏的样式
您可以使用SwiftUI中的NavigationView来实现这种左右toolbar栏的样式。以下是一个示例代码:
struct ContentView: View {
var body: some View {
NavigationView {
List {
Text("Item 1")
Text("Item 2")
Text("Item 3")
}
.navigationTitle("My List")
.toolbar {
ToolbarItemGroup(placement: .navigationBarLeading) {
Button(action: {
print("Tapped on leading button")
}) {
Image(systemName: "square.and.pencil")
}
Button(action: {
print("Tapped on another leading button")
}) {
Image(systemName: "trash")
}
}
ToolbarItemGroup(placement: .navigationBarTrailing) {
Button(action: {
print("Tapped on trailing button")
}) {
Image(systemName: "plus")
}
}
}
}
}
}
在这个示例中,我们创建了一个NavigationView,并在其中展示了一个列表。我们还为导航栏左侧和右侧添加了两个工具栏按钮。这些按钮被分组放置在ToolbarItemGroup中,并使用placement参数指定其放置位置。
您可以根据需要调整按钮的样式和功能,以满足您的需求
原文地址: https://www.cveoy.top/t/topic/f24k 著作权归作者所有。请勿转载和采集!