Custom ActionSheet doesn’t work when getting in SplitView
I want to implement a custom ActionSheet, everything works fine except one thing, when getting in split view the touch area of the button from my custom action sheet is wrongly positioned. As you can see in the demo(at the end of the demo) I press on the button but nothing happens, but if I press a little bit higher the button is pressed, it looks somehow that the touch area of the button is not over the button as it is supposed to be.
Here is a demo representing the problem:
Here is my code:
ContentView
import SwiftUI
struct ContentView: View {
var body: some View {
UIGeometryManager {
ModalContent()
}
}
}
ModalContent
import SwiftUI
struct ModalContent: View {
@EnvironmentObject var geometryManager: GeometryManager
@ObservedObject var actionSheetViewModel = ActionSheetViewModel.sharedInstance
var body: some View {
Text("")
.sheet(isPresented: Binding.constant(true)) {
ZStack(alignment: .bottom) {
Color.green.opacity(0.3)
VStack {
Text("Modal").padding(.bottom, 10)
Button(action: {
self.actionSheetViewModel.showActionSheet(geometryManager: self.geometryManager)
}) {
Text("Show ActionSheet").frame(width: 300, height: 50).background(Color.red)
}
.popover(isPresented: self.$actionSheetViewModel.showActionSheetRegular){
ActionSheetRegular()
}
Color.clear
}
ActionSheetCompact()
}
}
}
}
ActionSheetCompact
import SwiftUI
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())
}
}
}
ActionSheetRegular
import SwiftUI
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())
}
}
}
ActionSheetViewModel
import SwiftUI
import Combine
class ActionSheetViewModel: ObservableObject {
@Published var showActionSheetCompact = false
@Published var showActionSheetRegular = false
var cancellableSet: Set<AnyCancellable> = []
var geometryManager: GeometryManager?
static let sharedInstance = ActionSheetViewModel()
private init() {}
func showActionSheet(geometryManager: GeometryManager) {
self.geometryManager = geometryManager
self.geometryManager?.$horizontalSizeClass.sink {
if $0 == .regular {
self.showActionSheetCompact = false
self.showActionSheetRegular = true
} else {
self.showActionSheetRegular = false
self.showActionSheetCompact = true
}
}.store(in: &cancellableSet)
}
}
UIGeometryManager
import SwiftUI
struct UIGeometryManager<Content>: View where Content: View {
@Environment(.horizontalSizeClass) var horizontalSizeClass
let content: () -> Content
let geometryManager = GeometryManager()
var body: some View {
return GeometryReader { geometryProxy -> AnyView in
self.geometryManager.updateSizes(from: geometryProxy, for: self.horizontalSizeClass)
return AnyView(self.content().environmentObject(self.geometryManager))
}
}
}
GeometryManager
import SwiftUI
import Combine
class GeometryManager: ObservableObject {
@Published var horizontalSizeClass: UserInterfaceSizeClass?
private var cancellableSet: Set<AnyCancellable> = []
func updateSizes(from geometryProxy: GeometryProxy, for horizontalSizeClass: UserInterfaceSizeClass?) {
self.horizontalSizeClass = horizontalSizeClass
}
}
Source: Ios