我正在创建带有操作的图像按钮,但它们不会进入自己相应的视图。有人能指出我在下面的代码中做错了什么或遗漏了什么吗。谢谢你的帮助。下面是我的代码。我正在用ZStack包装它,但仍然没有工作。我看不见的东西少了什么?
struct DashboardView: View {
@State var showingNotifications = false
@State var showingMore = false
@State var showingARCam = false
@State var showingMapView = false
@State var showingProfile = false
var body: some View {
ZStack {
Button(action: {
withAnimation {
self.showingNotifications.toggle()
}
}) {
Image("NotificationButton").offset(x: -120, y: -180)
}.sheet(isPresented: $showingNotifications) {
NotificationView()
.edgesIgnoringSafeArea(.all)
}
Spacer()
Button(action: {
withAnimation {
self.showingMore.toggle()
}
}) {
Image("NotificationButton").offset(x: 80, y: -180)
}.sheet(isPresented: $showingMore) {
MoreView()
.edgesIgnoringSafeArea(.all)
}
Spacer()
Button(action: {
withAnimation {
self.showingARCam.toggle()
}
}) {
Image("AR").offset(x: 10, y: 65)
}.sheet(isPresented: $showingARCam) {
AR()
.edgesIgnoringSafeArea(.all)
}
Spacer()
Button(action: {
withAnimation {
self.showingMapView.toggle()
}
}) {
Image("MapIcon").offset(x: -120, y: 250)
}.sheet(isPresented: $showingMapView) {
MapView()
.edgesIgnoringSafeArea(.all)
}
Button(action: {
withAnimation {
self.showingProfile.toggle()
}
}) {
Image("ProfileIcon").offset(x: 90, y: 250)
}.sheet(isPresented: $showingProfile) {
ProfileView()
.edgesIgnoringSafeArea(.all)
}
}
.background(
Image("Dashboard")
.resizable()
.edgesIgnoringSafeArea(.all)
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
}
}
删除offsets
。。。.offset
修改器不改变视图的位置(也不影响布局(,而只改变绘图的位置,所以您可以在一个地方看到图像,但实际按钮位于不同的位置,因此敲击图像没有效果。
Button(action: {
withAnimation {
self.showingNotifications.toggle()
}
}) {
Image("NotificationButton") // .offset(x: -120, y: -180) // << here !!
}.sheet(isPresented: $showingNotifications) {