SwiftUI NavigationButton without the disclosure indicator?

前端 未结 16 841
不知归路
不知归路 2020-12-02 12:04

When making a List with a row that pushes to a new view, SwiftUI adds a disclosure indicator \">\" automatically? How do I remove it if I don\'t want it?

            


        
相关标签:
16条回答
  • 2020-12-02 12:49

    As workaround I can suggest to add .padding modifier like this:

    NavigationView {
            List {
                NavigationButton(destination: DetailView()) {
                    ListItem()
                }
            }
            .navigationBarTitle(Text("Some title"))
        }
        .padding(.trailing, -32.0)
    

    So you will get rows without visible disclosure:

    0 讨论(0)
  • 2020-12-02 12:49

    As of beta 6, this works well:

    struct SwiftUIView: View {   
        var body: some View {
    
            NavigationView {
                List {
                    HStack {
                        Text("My Cell Content")
                        NavigationLink(destination: Text("destination"), label: {
                            EmptyView()
                        })
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 12:50

    Removing List and just using ForEach works fine with navigation link. You just have to create your own list row. This works for me

    NavigationView {
       ForEach(pages) {
        page in
          NavigationLink(destination: DetailView()) {
             ListItem()
         }
       }
    }
    
    0 讨论(0)
  • 2020-12-02 12:51

    When using List() there is no way to remove disclosure indicator without workarounds. However there is another easy way to do want you want to do, without List(). Instead of a List(), you can use an ScrollView() which has a ForEach() in it.

    It is neither harder nor more time-consuming, But it makes disclosure indicators of a NavigationLink disappear with no trick or workarounds, at no additional cost! letting you easily navigate to another view with a nice normal button.

    0 讨论(0)
  • 2020-12-02 12:56

    Swift 5, Xcode 11. ZStack works perfect.

    var body: some View {
        NavigationView {
            List {
                ForEach(viewModel.currenciesViewModel) { cellViewModel in
                    ZStack {
                        cellViewModel.makeView()
                        NavigationLink(destination: ChooseCurrencyListView()) {
                            EmptyView()
                        }
                        .buttonStyle(PlainButtonStyle())
                    }
                }
            }
            .navigationBarHidden(true)
            .navigationBarTitle("", displayMode: .inline)
        }
    }
    
    0 讨论(0)
  • 2020-12-02 12:56

    NavigationLink is what we should define in a scope enclosed inside a NavigationView. But when we use NavigationLink it is attached to the enclosing view, so to reuse the same NavigationLink with other views, we use tag which differentiates between different Destinations.

    struct SwiftUIView: View {
    @State private var viewState: Int? = 0
    
    var body: some View {
    
        NavigationView {
                    VStack {
                        NavigationLink(destination: Text("View 1"), tag: 1, selection: $viewState) {
                            EmptyView()
                        }
    
                        NavigationLink(destination: Text("View 2"), tag: 2, selection: $viewState) {
                            EmptyView()
                        }
    
                        Text("First View")
                            .onTapGesture {
                                self.viewState = 1
                            }
    
                        Text("Second View")
                            .onTapGesture {
                                self.viewState = 2
                            }
                        }
                }
        }
    } 
    

    Here we bind a Hashable property with all the NavigationLinks present in our VStack so that when a particular View is tapped we can notify which Destination should be opened by setting the value of Bindable property. If we don't notify the correct Destination by setting the value of tag, always the View defined inside the Closure of NavigationLink will be clickable and nothing else.

    Using this approach you don't need to wrap all your clickable views inside NavigationView, any action on any view can use any NavigationLink just by setting the tag.

    Thanks, hope this helps.

    0 讨论(0)
提交回复
热议问题