我想实现一个自定义的 ActionSheet,除了一件事,当进入拆分视图并打开操作表时,我的自定义操作表中的按钮的触摸区域位置错误。正如您在演示中看到的(在它的末尾)我按下按钮但没有任何反应,但如果我按下稍微高一点的按钮被按下,它看起来按钮的触摸区域不在按钮上方应该是这样。
这是一个代表问题的演示:
这是我的代码:
import SwiftUI
struct ContentView: View {
var body: some View {
ModalContent()
}
}
struct ModalContent: View {
@ObservedObject var actionSheetViewModel = ActionSheetViewModel.sharedInstance
@Environment(\.horizontalSizeClass) var horizontalSizeClass
var body: some View {
if horizontalSizeClass == .regular {
if actionSheetViewModel.showActionSheetCompact != false && actionSheetViewModel.showActionSheetRegular != true {
actionSheetViewModel.showActionSheetCompact = false
actionSheetViewModel.showActionSheetRegular = true
}
} else {
if actionSheetViewModel.showActionSheetCompact != true && actionSheetViewModel.showActionSheetRegular != false {
actionSheetViewModel.showActionSheetRegular = false
actionSheetViewModel.showActionSheetCompact = true
}
}
return Text("")
.sheet(isPresented: Binding.constant(true)) {
ZStack(alignment: .bottom) {
Color.green.opacity(0.3)
VStack {
Text("Modal").padding(.bottom, 10)
Button(action: {
self.actionSheetViewModel.showActionSheetCompact = true
}) {
Text("Show ActionSheet").frame(width: 300, height: 50).background(Color.red)
}
.popover(isPresented: self.$actionSheetViewModel.showActionSheetRegular){
ActionSheetRegular()
}
Color.clear
}
ActionSheetCompact()
}
}
}
}
struct ActionSheetCompact: View {
@ObservedObject var actionSheetViewModel = ActionSheetViewModel.sharedInstance
var body: some View {
if actionSheetViewModel.showActionSheetCompact == true {
return AnyView(
VStack {
Color.clear
Button(action: {
print("Action sheet button was pressed")
}) {
Text("Action Sheet").frame(width: 100, height: 45).background(Color.yellow)
}
})
} else {
return AnyView(EmptyView())
}
}
}
struct ActionSheetRegular: View {
@ObservedObject var actionSheetViewModel = ActionSheetViewModel.sharedInstance
var body: some View {
if actionSheetViewModel.showActionSheetRegular == true {
return AnyView(
VStack {
Color.clear
Button(action: {
print("Action sheet button was pressed")
}) {
Text("Action Sheet").frame(width: 100, height: 45).background(Color.yellow)
}
})
} else {
return AnyView(EmptyView())
}
}
}
class ActionSheetViewModel: ObservableObject {
@Published var showActionSheetCompact = false
@Published var showActionSheetRegular = false
static let sharedInstance = ActionSheetViewModel()
private init() {}
}