Categories
Apple Apps Programiranje Programming Technology

SwiftUI swipe back gesture bug with bottom toolbar

As we are using more and more of SwiftUI every day I noticed one strange bug. The bug is not a critical but it is quite annoying.

In one of the previous iOS versions Apple added ability to use swipe/drag gesture to navigate back when using a standard navigation controller. It is quite useful because screens are bigger and that back button is on top, so it is much easier just to swipe to go back.

In one of my apps I have one screen where I have a List inside of NavigationView and it has a bottom Toolbar with some content. One day I tried to do the swipe back gesture and I noticed a weird bug. Even though I did not finish the gesture my navigation bar was updated to content of the target screen (the screen where the back gesture would take me back to).

I was debugging like crazy, could not find a reason why it was happening. One day I tried to comment out my bottom toolbar content and wow, it worked. For some reason when having a bottom toolbar it does not work properly. If you place the toolbar content in other positions it works correctly, at least for the other positions I tested.

Here is a dummy sample code showing the difference:

import SwiftUI
struct ContentView: View {
    
    private let entries: [String] = ["first", "second", "third"]
    
    var body: some View {
        TabView {
            // Tab item WITHOUT a toolbar
            NavigationView {
                listContent
            }
            .tabItem {
                Text("No Toolbar")
            }
            // Tab item WITH a toolbar
            NavigationView {
                listContent
                .toolbar {
                    ToolbarItemGroup(placement: .bottomBar) {
                        Text("Toolbar content")
                    }
                }
            }
            .tabItem {
                Text("Toolbar")
            }
        }
        
    }
    
    @ViewBuilder
    private var listContent: some View {
        List {
            ForEach(entries, id: \.self) { entry in
                NavigationLink(destination: Text(entry).navigationTitle(entry)) {
                    Text(entry)
                }
            }
        }
        .navigationTitle("Title")
    }
}
#Preview {
    ContentView()
}

I also filed a bug to Apple (FB13333930) so I will update this post when we get some feedback back.

Hopefully this gets resolved soon and this post helps somebody else trying to figure out why is this happening. 😉

Leave a Reply

Your email address will not be published. Required fields are marked *