简体   繁体   English

如果 NavigationLink 不可见,如何使 NavigationLink 工作,SwiftUI?

[英]How to make NavigationLink work if it is not visible, SwiftUI?

When using NavigationLink on the bottom of a view after ForEach it won't work if it is not visible.在 ForEach 之后在视图底部使用 NavigationLink 时,如果它不可见,它将不起作用。

I have a list of Buttons.我有一个按钮列表。 If a button is pressed, it sets a Bool to true.如果按下按钮,它会将 Bool 设置为 true。 This bool value now shows a NavigationLink which immediately activates because the passed binding is set to true.这个 bool 值现在显示一个 NavigationLink,它会立即激活,因为传递的绑定设置为 true。 However, the link won't work if the array is too long because it will be out of sight once one of the first buttons is pressed.但是,如果数组太长,链接将不起作用,因为一旦按下第一个按钮,它就会消失。

This is my Code:这是我的代码:

import SwiftUI

struct TestLinkView: View {
    @State private var linkIsActive = false
    
    var body: some View {
        NavigationView {
            VStack {
                Button(action: {
                    linkIsActive = true
                }) {
                    Text("Press")
                }
                
                NavigationLink(destination: ListView(linkIsActive: $linkIsActive), isActive: $linkIsActive) {
                    Text("Navigation Link")
                }
            }
        }
    }
}


struct ListView: View {
    
    var nameArray = ["Name1","Name2","Name3","Name4","Name5","Name6","Name7","Name8","Name9","Name10","Name11","Name12","Name13","Name14","Name15","Name16","Name17","Name18","Name19","Name20" ]
    @State private var showLink: Bool = false
    @State private var selectedName: String = ""
    @Binding var linkIsActive: Bool
    var body: some View {
        
        Form {
            ForEach(nameArray, id: \.self) { name in
                Button(action: {
                    selectedName = name
                    showLink = true
                }) {
                    Text(name)
                }
            }
            
            if showLink {
                NavigationLink(destination: NameView(selectedName: selectedName), isActive: $linkIsActive) {
                    EmptyView()
                }
            }
        }
        .navigationBarTitle("ListView")
    }
}

struct NameView: View {
    
    var selectedName: String
    
    var body: some View {
        Text(selectedName)
            .navigationBarTitle("NameView")
    }
}

What would work is to pass the NavigationLink with the if-condition inside the button label.有效的方法是在按钮标签内传递带有 if 条件的 NavigationLink。 However if I do that, the animation won't work anymore.但是,如果我这样做,动画将不再起作用。

You don't need it in Form, which is like a List don't create views far outside of visible area.您不需要在 Form 中使用它,这就像 List 不要在可见区域之外创建视图一样。 In your case the solution is to just move link into background of Form (because it does not depend on form internals).在您的情况下,解决方案是将链接移动到表单的背景中(因为它不依赖于表单内部)。

The following tested as worked with Xcode 12 / iOS 14.以下测试与 Xcode 12 / iOS 14 一起使用。

    Form {
        ForEach(nameArray, id: \.self) { name in
            Button(action: {
                selectedName = name
                showLink = true
            }) {
                Text(name)
            }
        }
    }
    .background(Group{
        if showLink {
            NavigationLink(destination: NameView(selectedName: selectedName), isActive: $linkIsActive) {
                EmptyView()
            }
        }
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM