in my ContentView, I have something analogous to this:
import SwiftUI
struct MainContentView : View {
var body: some View {
Text("Main Content View")
}
}
struct AlternateView : View {
var body: some View {
Text("Alternate View")
}
}
struct ContentView : View {
@State private var selection: String? = "Main"
var body: some View {
ZStack {
NavigationView {
VStack {
ZStack {
Color.clear
AlternateView()
.navigationBarHidden(true)
.edgesIgnoringSafeArea(.top)
//button to go back to the MainContentView()
//AlternateViewBackButton(button_action: self.hide_alternate_view)
}
NavigationLink(destination:
MainContentView()
//why cant I do this without breaking NavigationView?
//.navigationBarBackButtonHidden(true)
,
tag: "Main",
selection: $selection)
{
EmptyView()
}
.navigationBarHidden(true)
.navigationBarTitle("")
.navigationBarBackButtonHidden(true)
}
}
.edgesIgnoringSafeArea(.all)
}
}
}
My question is in the code commented above. Why does adding navigationBarBackButtonHidden(true) break the functionality where I can drag from the left of the screen to the right to animate between the two views? Is this something I am doing wrong? or is it a SwiftUI bug?
I encountered a solution which I thought would fix this problem, namely: Hide navigation bar without losing swipe back gesture in SwiftUI It works on the test case I have written above, but fails on the full program unless I deactivate all of the gestures I have installed on MainContentView with .simultaneousGesture
This partial solution may be included over the original code example with the following snippet (copied from the linked post)
extension UINavigationController: UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}
It seems to work on the test example, but when I use it in my main project, it interferes with the gestures I have installed on MainContentView. I will try to figure out exactly why this is happening and include what I find below.
-edit-
The way this problem manifests in my codebase is as follows:
If I include the extension code snippet above, uncomment .navigationBarBackButtonHidden(true), and comment all gestures on MainContentView (including every one of its children), There is no back button (as intended) and I can drag from left to right to access the AlternateView (as intended). However, commenting the gestures on MainContentView and children is not possible, as those govern the core functionality of the application.
If I knew why this was happening, I would not have a question anymore. It seems to me that .navigationBarBackButtonHidden should not change anything other than the fact that the back button is hidden.
-end of edit-
I need to be able to handle gestures on both MainContentView and AlternateView. Handling user input is a critical part of the program, after all.
One correlated question is this: why does the above code hide the navigationBar in portrait mode, but show it in landscape?
Thanks in advance for any help!