Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
                }
            })
            
        }
    }
}
like image 911
Ufuk Köşker Avatar asked Feb 02 '21 20:02

Ufuk Köşker


2 Answers

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()}
 )
like image 53
Sergei Volkov Avatar answered Nov 20 '22 23:11

Sergei Volkov


ForEach(items) { item in
            NavigationLink(tag: item.id, selection: $selection) {
                DetailView(selection: $selection, item: item)
            } label: {
                Text("\(item)")
            }
        }
like image 23
K999999999 Avatar answered Sep 27 '22 16:09

K999999999