I am working on a project where I have a custom navigationBar, which means I need to manage the back button and swipe back gesture myself. I didn't experience any problems in the past, but now when I try to swipe back and stop in the middle of the gesture (half swipe), the screen shows a top white space, as if the navigation bar is shown with no title (this problem doesn't occur if a custom back button is pressed or if the gesture is completed i.e. full swipe).
There is also an error displayed when trying to swipe back after the white space appears: changing items while animating can result in a corrupted navigation bar
.
The weird thing is that the problem only happens for the third screen from the NavigationView.
I attached a video which presents the problem, showing that half-swiping the SecondView
doesn't produce any top space, but half-swiping the ThirdView
does.
Here is the code:
import SwiftUI
// https://stackoverflow.com/questions/59921239/hide-navigation-bar-without-losing-swipe-back-gesture-in-swiftui
extension UINavigationController {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = nil
}
}
struct ContentView: View {
init() {
print("ContentView")
}
var body: some View {
NavigationView {
NavigationLink(destination: SecondView()) {
Text("Go to second view")
}
.navigationBarTitle("")
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
}
struct SecondView: View {
init() {
print("SecondView")
}
var body: some View {
VStack {
Text("Second view")
VStack {
NavigationLink(destination: ThirdView()) {
Text("Go to third view")
}
}
.padding(.horizontal, 20)
.padding(.top, 20)
Spacer()
}
.navigationBarTitle("")
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
struct ThirdView: View {
init() {
print("ThirdView")
}
var body: some View {
VStack {
Text("Third view")
Spacer()
}
.navigationBarTitle("")
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
EDIT: This can only be reproduced when using iOS 15, everything works fine in iOS 14.5