Home > Mobile >  NavigationView Pushing aNavigationLink Issue
NavigationView Pushing aNavigationLink Issue

Time:12-31

I have a horizontal menu with NavigationView and NavigationLink. The following is code.

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            ZStack {
                VStack {
                    HorizontalView()
                    Spacer()
                }
            }
        }
    }
}

struct MenuModel: Hashable, Identifiable {
    let id: Int
    let name: String
}

struct HorizontalView: View {
    @State var horizonModels = [MenuModel]()
    @State var isShowing = false
    @State var destIndex: Int = 0
    
    var body: some View {
        VStack {
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 30.0) {
                    ForEach(horizonModels) { item in
                        NavigationLink(isActive: $isShowing) {
                            DestView(destIndex: destIndex)
                        } label: {
                            Button(action: {
                                destIndex = item.id
                                isShowing = true
                            }, label: {
                                Text(item.name)
                            }).foregroundColor(.white)
                        }
                    }
                }
                .padding(.horizontal, 20.0)
                .frame(height: 36.0)
                .background(Color.red)
            }
            .navigationBarTitleDisplayMode(.inline)
            .navigationViewStyle(StackNavigationViewStyle())
            
            Spacer()
        }.onAppear {
            horizonModels = [
                MenuModel.init(id: 0, name: "Apple"),
                MenuModel.init(id: 1, name: "Banana"),
                MenuModel.init(id: 2, name: "Peach"),
                MenuModel.init(id: 3, name: "Grape"),
                MenuModel.init(id: 4, name: "Pineapple"),
                MenuModel.init(id: 5, name: "Watermelon"),
                MenuModel.init(id: 6, name: "Blueberry")
            ]
        }
    }
}

struct DestView: View {
    @State var destIndex: Int = 0
    var body: some View {
        if destIndex == 0 {
            // goigng to another view...
        }
        else {
            // goigng to another view...
        }
    }
}

And the screen looks like this picture. Everything looks all right except that the simulator says the following.

SwiftUI encountered an issue when pushing aNavigationLink. Please file a bug.

Is this something that I need to worry about? I find one topic here that might be related this issue, which doesn't quite look helpful to me.

Thanks.

CodePudding user response:

I don't know why you get the warning, but you could use this approach, to make it go away:

NavigationLink(destination: DestView(destIndex: item.id)) {
          Text(item.name).foregroundColor(.white)
}
  • Related