.navigationBarBackButtonHidden is delayed when using NavigationStack.count

Can someone help me understand the behavior of ‘NavigationStack.count’. I don’t understand how/when it’s updated. In this example, I use the count to disable back navigation. But as you can see, initially(as expected), the back button is disabled on screen 1. But when I navigate to screen 2, and then back to screen 1, the back button flashes for less than a second even though the print statement shows that count is 1 and should disable back navigation.

import SwiftUI

struct ExploreDelayedNavigationPathCount: View {
    @State private var navigationPath = NavigationPathWrapper()
    init() {
        navigationPath.path.append(0)
    }
    var body: some View {
        NavigationStack(path: $navigationPath.path) {
            Text("View to register navigationDestination only")
                .navigationDestination(for: Int.self) { number in
                    SubView(number: number, navigationPath: navigationPath)
                }
                .navigationTitle("Hidden Root")
        }
    }


}

struct SubView: View {
    let number: Int
    var navigationPath: NavigationPathWrapper
    var body: some View {
        Text("Go to screen (number + 1)")
            .onTapGesture {
                navigationPath.append(number + 1)
            }
            .navigationTitle("Screen (number)")
            .navigationBarBackButtonHidden(navigationPath.disableBackNavigation)
    }


}

#Preview {
    ExploreDelayedNavigationPathCount()
}

@Observable
class NavigationPathWrapper {
    var path = NavigationPath()
    var disableBackNavigation: Bool {
        print("path.count (path.count)")
        return path.count == 1
    }
    func append(_ value: any Hashable) {
        path.append(value)
    }
}

1

SwiftUI seems to simply update the view too late, probably at around viewDidAppear in terms of the UIKit lifecycle.

As a workaround, I would use a special type as the “first” destination, s that you can write another navigationDestination(for:) then only hide the back button in there.

enum NoBackButtonDestination {
    case unit
}

struct ContentView: View {
    @State private var navigationPath = NavigationPath()
    
    var body: some View {
        NavigationStack(path: $navigationPath) {
            Text("View to register navigationDestination only")
                .navigationDestination(for: Int.self) { number in
                    SubView(number: number, navigationPath: $navigationPath)
                }
                .navigationDestination(for: NoBackButtonDestination.self) { _ in
                    SubView(number: 0, navigationPath: $navigationPath)
                        .navigationBarBackButtonHidden(true)
                }
                .navigationTitle("Hidden Root")
        }
        .onAppear {
            // append the first value in onAppear!
            // don't do this in init, because init is called every time the parent updates
            navigationPath.append(NoBackButtonDestination.unit)
        }
    }
}

struct SubView: View {
    let number: Int
    @Binding var navigationPath: NavigationPath
    var body: some View {
        Text("Go to screen (number + 1)")
            .onTapGesture {
                navigationPath.append(number + 1)
            }
            .navigationTitle("Screen (number)")
    }
}

If “polluting” the navigation path like this is undesirable for some reason, it is also possible to set the back button to hidden on the UIKit side:

struct BackButtonHider: UIViewControllerRepresentable {
    
    class VC: UIViewController {
        
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            // '== 2' instead of '== 1' here because the root view controller is also included in the 'viewControllers'
            parent?.navigationItem.hidesBackButton = navigationController?.viewControllers.count == 2
        }
    }
    
    func makeUIViewController(context: Context) -> VC { .init() }
    
    func updateUIViewController(_ uiViewController: VC, context: Context) { }
}

struct SubView: View {
    let number: Int
    @Binding var navigationPath: NavigationPath
    var body: some View {
        Text("Go to screen (number + 1)")
            .onTapGesture {
                navigationPath.append(number + 1)
            }
            .navigationTitle("Screen (number)")
             // add this as the background
            .background { BackButtonHider() }
    }
}

This depends on the fact that the parent of the UIViewControllerRepresentable is the hosting controller that SwiftUI uses to host the navigation destination. In the unlikely event that SwiftUI stops using a UINavigationController to implement its NavigationStack, this will no longer work. But then navigationBarBackButtonHidden might start to work as you expect too, so who knows…

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật