gpt4 book ai didi

swift - SwiftUI 中 macOS 的侧边栏菜单

转载 作者:行者123 更新时间:2023-12-03 20:46:01 32 4
gpt4 key购买 nike

我正在尝试实现一个菜单,到目前为止,这是:
enter image description here
导航 View

struct macOS_NavigationView: View {

@State private var selectedTab: HostingBarCategories = .Screen1

var body: some View {

NavigationView {
// SideBar Menu
List {
ForEach(1 ... 10, id: \.self) { index in
NavigationLink(destination:
Text("\(index)")
.frame(maxWidth: .infinity, maxHeight: .infinity)
) {
Text("Link \(index)")
}
}
}
.listStyle(SidebarListStyle())

// Primary View
Text("Select a menu...")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
我被卡住的部分是尝试在 TabBar 中实现我用于 iOS 的当前模型:
HostingBarCategories
enum HostingBarCategories: Hashable {
case Screen1
case Screen2
case Screen3
case Screen4
case Screen5
}
那么我如何使用该模型,以便当用户单击菜单时它会转到该屏幕? (模型是可以扩展的,不一定是那个)
编辑:让我添加当前的iOS TabBar,以便在视觉上更容易理解,这仅供引用,与问题无关:
struct iOS_TabBarView: View {

@State private var selectedTab: HostingBarCategories = .Screen1

var body: some View {
TabView(selection: $selectedTab) {
Text("1")
.tag(0)
.tabItem {
Image(systemName: "pencil.and.outline")
Text("1")
}
Text("2")
.tag(1)
.tabItem {
Image(systemName: "checkmark")
Text("2")
}
Text("3")
.tag(2)
.tabItem {
Image(systemName: "calendar.circle.fill")
Text("3")
}
Text("4")
.tag(3)
.tabItem {
Image(systemName: "flame")
Text("4")
}
Text("5")
.tag(3)
.tabItem {
Image(systemName: "slider.horizontal.3")
Text("5")
}
}
}
}

最佳答案

您需要使您的枚举大小写可迭代以将其用作 ForEach 中的模型, 喜欢

enum HostingBarCategories: Hashable, CaseIterable {
case Screen1
case Screen2
case Screen3
case Screen4
case Screen5

var string: String { String(describing: self) }
}

struct macOS_NavigationView: View {

@State private var selectedTab: HostingBarCategories = .Screen1

var body: some View {

NavigationView {
// SideBar Menu
List {
ForEach(HostingBarCategories.allCases, id: \.self) { screen in
NavigationLink(destination:
Text(screen.string)
.frame(maxWidth: .infinity, maxHeight: .infinity)
) {
Text("Link \(screen.string)")
}
}
}
.listStyle(SidebarListStyle())

// Primary View
Text("Select a menu...")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}

关于swift - SwiftUI 中 macOS 的侧边栏菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65570665/

32 4 0