我不明白为什么点击按钮没有出现额外的视图
struct ContentView: View {
@State private var isGlassesMenuShow = false
@State public var bgColor = Color.gray
var body: some View {
HStack{
VStack{
SceneView(scene: getSirenHead(), options: [.autoenablesDefaultLighting,.allowsCameraControl]).frame(width:UIScreen.main.bounds.width - 50, height: UIScreen.main.bounds.height-100)
GlassView(isOn: $isGlassesMenuShow)
}
Button("", action: {
self.isGlassesMenuShow = true
})}
这是视图类
struct GlassView: View {
@Binding var isOn: Bool
var body: some View {
if isOn{
ScrollView(.horizontal) {
HStack{
ZStack{
Button("", action: {
}).frame(width: 100, height: 100).background(Color.white)
Image("glass")
.resizable().frame(width: 100, height: 50)}
ZStack{
Button("", action: {
}).frame(width: 100, height: 100).background(Color.white)
Image("glass2")
.resizable().frame(width: 100, height: 50)}
}}
}
else{
EmptyView()
}
} }
当按下按钮时,所需的视图根本不会出现,但是当您在代码中手动设置为 true 时,它就会出现
答案 0 :(得分:-1)
它确实有效,当您按下“单击我”按钮时,会出现 GlassView。 这是我的测试代码。
struct ContentView: View {
@State private var isGlassesMenuShow = false
@State public var bgColor = Color.gray
var body: some View {
HStack{
VStack{
Text("testing testing")
GlassView(isOn: $isGlassesMenuShow)
}
Button("click me", action: { // <--- press this button and GlassView appears
self.isGlassesMenuShow = true
})}
}
}
struct GlassView: View {
@Binding var isOn: Bool
var body: some View {
if isOn{
ScrollView(.horizontal) {
HStack{
ZStack{
Button("button1", action: {
}).frame(width: 100, height: 100).background(Color.white)
Image(systemName: "globe").resizable().frame(width: 100, height: 50)}
ZStack{
Button("button2", action: {
}).frame(width: 100, height: 100).background(Color.white)
Image(systemName: "info").resizable().frame(width: 100, height: 50)}
}}
}
else{
EmptyView()
}
}
}