2

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.

enter image description here

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

Liviu
  • 142
  • 4
  • 11

1 Answers1

1

It's some weird hackery, but I was able to "fix" it by using .navigationViewStyle(.stack) on NavigationView.

If you are interested, btw, the white space was navigation bar and large navigation title. A useful thing in debugging this kind of stuff is the "Debug View Hierarchy" button. Arrow showing "Debug View Hierarchy" button on debug panel in Xcode 13.1

It's definitely a hack, but you could try submitting a bug report to Apple anyway.

eXCore
  • 296
  • 2
  • 8
  • it is not so clear. two of my consecutive screens were with .navigationViewStyle(.stack) and I had to actually remove the navigationViewStyle(.stack) from the first screen to make it work - seems like a real bug from apple side – vigdora Mar 30 '23 at 09:19