Ufuk Köşker
Ufuk Köşker

Reputation: 1480

SwiftUI NavigationLink Bug (SwiftUI encountered an issue when pushing aNavigationLink. Please file a bug.)

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

There is no problem when I don't use the isActive parameter in NavigationLink. However, I have to use the isActive parameter. Because I'm closing the drop-down list accordingly.

Menu Model:

struct Menu: Identifiable {
    var id: Int
    var pageName: String
    var icon: String
    var page: Any
    var startDelay: Double
    var endDelay: Double
//    var offsetY: CGFloat
}

let menu = [
    Menu(id: 1, pageName: "Profil", icon: "person.crop.circle", page: ProfileView(), startDelay: 0.2, endDelay: 0.6),
    Menu(id: 2, pageName: "Sepet", icon: "cart", page: CartView(), startDelay: 0.4, endDelay: 0.4),
    Menu(id: 3, pageName: "İstek", icon: "plus.circle", page: ClaimView(), startDelay: 0.6, endDelay: 0.2)
]

MenuView

struct MenuView: View {
    @State var isShownMenu: Bool = false
    @State var isPresented: Bool = false
    var body: some View {
        VStack(spacing: 40) {
            
            Button(action: {self.isShownMenu.toggle()}) {
                MenuViewButton(page: .constant((Any).self), icon: .constant("rectangle.stack"))
            }
            VStack(spacing: 40) {
                ForEach(menu, id: \.id) { item in
                    
                    NavigationLink(
                        destination: AnyView(_fromValue: item.page),
                        isActive: self.$isPresented,
                        label: {

                            MenuViewButton(page: .constant(item.page), icon: .constant(item.icon))
                        .animation(Animation.easeInOut.delay(self.isShownMenu ? item.startDelay : item.endDelay))
                        .offset(x: self.isShownMenu ? .zero : UIScreen.main.bounds.width)//, y: item.offsetY)
                }
            }
            .onChange(of: isPresented, perform: { value in
                if value == true {
                    self.isShownMenu = false
                }
            })
            
        }
    }
}

Upvotes: 17

Views: 11226

Answers (3)

Russ
Russ

Reputation: 576

I got this when I accidentally had two NavigationLinks with the same isActive boolean.

Upvotes: 2

K999999999
K999999999

Reputation: 9

ForEach(items) { item in
            NavigationLink(tag: item.id, selection: $selection) {
                DetailView(selection: $selection, item: item)
            } label: {
                Text("\(item)")
            }
        }

Upvotes: 0

Sergei Volkov
Sergei Volkov

Reputation: 1154

The problem is that you have NavigationLink with the "IsActive" parameter placed in the ForEach cycle! You need to remove NavigationLink from the cycle and transfer the necessary data there, for example, through the view model.

Summary: you should only have one NavigationLink associated with one specific isActive parameter.

 ForEach(yourData) { dataItem in
     Button {
         selectedItem = dataItem
         isActivated = true
     } label: {
         Text("\(dataItem)")            
     }
 }
 .background(
     NavigationLink(destination: DestinationView(data: selectedItem),
                    isActive: $isActivated) {EmptyView()}
 )

Upvotes: 40

Related Questions