如何使用一个按钮一次显示 VStack 中 2 个结构的内容?
How to show contents of 2 structures in a VStack one at a time using one button?
我试图在不同时间显示一个结构 PersonalSchedule 和另一个结构 EveryoneSchedule,同时使用一个按钮。以前,我有一个菜单,但我发现如果只有 2 个选项,菜单毫无意义,我想我可以制作一个按钮来执行它并来回切换。格式和一切都在那里,但没有连接,我做错了什么?
import SwiftUI
struct Schedule: View {
@State var isPresentedSideMenu = false
@State var width = UIScreen.main.bounds.width
init() {
UITabBar.appearance().isHidden = true
}
@ State var scheduleType = "pschedule"
var body: some View {
ZStack {
Color("Background")
.edgesIgnoringSafeArea(.all)
VStack(alignment: .leading) {
VStack(alignment: .leading) {
HStack(spacing: 80) {
Text("Schedule")
.font(.largeTitle)
.foregroundColor(.labelColor)
Button(action: {self.scheduleType = "pschedule"}, label: {
Text("Schedule Types")
.foregroundColor(.labelColor)
})
.padding(.vertical, 10)
.padding(.horizontal)
.background(Color.newSecondaryColor)
.clipShape(Capsule())
.frame(width: 160, height: 20, alignment: .trailing)
.shadow(radius: 7)
}
Text("View your schedule, make shift changes, and view everyone else's schedule. So you always know who you're working with.")
.font(.body)
.foregroundColor(.labelColor)
}
VStack(alignment: .leading) {
if self.scheduleType == "pschedule" {
PersonalSchedule()
} else {
EveryoneSchedule()
}
}
}.padding(.all, 10)
}
}
如果只是if/else
,那么做成boolean就更简单了,比如
struct Schedule: View {
@State var isPresentedSideMenu = false
@State var width = UIScreen.main.bounds.width
init() {
UITabBar.appearance().isHidden = true
}
@State var isPersonSchedule = true // << here !!
var body: some View {
ZStack {
Color("Background")
.edgesIgnoringSafeArea(.all)
VStack(alignment: .leading) {
VStack(alignment: .leading) {
HStack(spacing: 80) {
Text("Schedule")
.font(.largeTitle)
.foregroundColor(.labelColor)
Button(action: {
self.isPersonSchedule.toggle() // << here !!
}, label: {
Text("Schedule Types")
.foregroundColor(.labelColor)
})
.padding(.vertical, 10)
.padding(.horizontal)
.background(Color.newSecondaryColor)
.clipShape(Capsule())
.frame(width: 160, height: 20, alignment: .trailing)
.shadow(radius: 7)
}
Text("View your schedule, make shift changes, and view everyone else's schedule. So you always know who you're working with.")
.font(.body)
.foregroundColor(.labelColor)
}
VStack(alignment: .leading) {
if self.isPersonSchedule { // << here !!
PersonalSchedule()
} else {
EveryoneSchedule()
}
}
}.padding(.all, 10)
}
}
}
我试图在不同时间显示一个结构 PersonalSchedule 和另一个结构 EveryoneSchedule,同时使用一个按钮。以前,我有一个菜单,但我发现如果只有 2 个选项,菜单毫无意义,我想我可以制作一个按钮来执行它并来回切换。格式和一切都在那里,但没有连接,我做错了什么?
import SwiftUI
struct Schedule: View {
@State var isPresentedSideMenu = false
@State var width = UIScreen.main.bounds.width
init() {
UITabBar.appearance().isHidden = true
}
@ State var scheduleType = "pschedule"
var body: some View {
ZStack {
Color("Background")
.edgesIgnoringSafeArea(.all)
VStack(alignment: .leading) {
VStack(alignment: .leading) {
HStack(spacing: 80) {
Text("Schedule")
.font(.largeTitle)
.foregroundColor(.labelColor)
Button(action: {self.scheduleType = "pschedule"}, label: {
Text("Schedule Types")
.foregroundColor(.labelColor)
})
.padding(.vertical, 10)
.padding(.horizontal)
.background(Color.newSecondaryColor)
.clipShape(Capsule())
.frame(width: 160, height: 20, alignment: .trailing)
.shadow(radius: 7)
}
Text("View your schedule, make shift changes, and view everyone else's schedule. So you always know who you're working with.")
.font(.body)
.foregroundColor(.labelColor)
}
VStack(alignment: .leading) {
if self.scheduleType == "pschedule" {
PersonalSchedule()
} else {
EveryoneSchedule()
}
}
}.padding(.all, 10)
}
}
如果只是if/else
,那么做成boolean就更简单了,比如
struct Schedule: View {
@State var isPresentedSideMenu = false
@State var width = UIScreen.main.bounds.width
init() {
UITabBar.appearance().isHidden = true
}
@State var isPersonSchedule = true // << here !!
var body: some View {
ZStack {
Color("Background")
.edgesIgnoringSafeArea(.all)
VStack(alignment: .leading) {
VStack(alignment: .leading) {
HStack(spacing: 80) {
Text("Schedule")
.font(.largeTitle)
.foregroundColor(.labelColor)
Button(action: {
self.isPersonSchedule.toggle() // << here !!
}, label: {
Text("Schedule Types")
.foregroundColor(.labelColor)
})
.padding(.vertical, 10)
.padding(.horizontal)
.background(Color.newSecondaryColor)
.clipShape(Capsule())
.frame(width: 160, height: 20, alignment: .trailing)
.shadow(radius: 7)
}
Text("View your schedule, make shift changes, and view everyone else's schedule. So you always know who you're working with.")
.font(.body)
.foregroundColor(.labelColor)
}
VStack(alignment: .leading) {
if self.isPersonSchedule { // << here !!
PersonalSchedule()
} else {
EveryoneSchedule()
}
}
}.padding(.all, 10)
}
}
}