I found a question like this on Stack Overflow however it did not seem to work. Maybe I'm implementing it wrong, or it no longer works. I have a @Binding showDetailView that when equals true, shows the detail view.
This detail view has a custom back button that works. I want to be able to do a swipe gesture that performs the same function as the back button.
The code I found that looks like it could help is:
extension UINavigationController: UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}
The code for my main view (product page) is:
struct ProductPage: View {
@StateObject var MarketplaceModel = MarketplaceViewModel()
@State private var selectedMarketplaceFilter: MarketplaceFilterViewModel = .productList
@Namespace var animation : Namespace.ID
@State var showDetailProduct = false
@State var selectedProduct : Product!
var body: some View {
var columns = Array(repeating: GridItem(.flexible()), count: 2)
ZStack{
VStack(spacing: 10){
if MarketplaceModel.products.isEmpty{
Spacer()
ProgressView()
Spacer()
}
else{
ScrollView(.vertical, showsIndicators: false, content: {
LazyVGrid(columns: Array(repeating: GridItem(.flexible(),spacing: 10), count: 2),spacing: 20){
ForEach(MarketplaceModel.filteredProduct){product in
ProductView(productData: product)
.onTapGesture {
withAnimation {
selectedProduct = product
showDetailProduct.toggle()
}
}
}
}
})
}
}
if selectedProduct != nil && showDetailProduct{
ProductDetailView(showDetailProduct: $showDetailProduct, productData: selectedProduct, product_id)
.transition(.move(edge: .trailing))
}
}
}
}
The code for my detail view (product detail view) is:
struct ProductDetailView: View {
@StateObject var MarketplaceModel = MarketplaceViewModel()
@Binding var showDetailProduct: Bool
@Namespace var animation: Namespace.ID
@EnvironmentObject var marketplaceData: MarketplaceViewModel
var productData : Product
var product_id: String
var body: some View {
NavigationView{
VStack{
VStack{
// Title Bar...
HStack {
Button(action: {
withAnimation{showDetailProduct.toggle()}
}) {
Image(systemName: "arrow.backward.circle.fill")
Spacer()
ForEach(MarketplaceModel.product_details_array){ items in
Text(items.product_name)
.font(.largeTitle)
.fontWeight(.heavy)
.foregroundColor(.black)
}
}
}
ScrollView {
VStack {
Text(productData.product_name)
Text(productData.product_details)
}
}
}
}
}
}
}
How would I go about adding the back swipe gesture? (Running Xcode 13.4.1)