SwiftUI NavigationButton without the disclosure indicator?

前端 未结 16 839
不知归路
不知归路 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:37

    just came here looking for the answer to this question, but none of the proposed solutions worked for me (can't have an empty view, because i want to put something in the list row; i'm already messing with the padding (and increasing trailing padding didn't seem to work) ... i was about to give up, and then something occurred to me: what if you crank up the z-index of the list row itself? seemed somewhat unlikely, but i gave it a try and, i'll be damned, it worked! i was so pleasantly surprised, i felt like sharing ...

    e.g.:

    // in body of your list row view
    HStack(alignment: .top, spacing: 0.0) {
        // stuff ...
    }
    .zIndex(9999999999)
    
    0 讨论(0)
  • 2020-12-02 12:38

    You don't have to use NavigationLink to wrap your Label directly. It will work as long as the link is anywhere in your view hierarchy.

    Here I've wrapped it in a button, which allows you to trigger an action prior to pushing the view. Since the NavigationLink has an EmptyView for the label the disclosure indicator is not visible. You can also style this with ButtonStyle.

    struct NavigationButton<Destination: View, Label: View>: View {
        var action: () -> Void = { }
        var destination: () -> Destination
        var label: () -> Label
    
        @State private var isActive: Bool = false
    
        var body: some View {
            Button(action: {
                self.action()
                self.isActive.toggle()
            }) {
                self.label()
                  .background(NavigationLink(destination: self.destination(), isActive: self.$isActive) {
                      EmptyView() 
                   })
            }
        }
    }
    

    And to use it:

    NavigationButton(
        action: { print("tapped!") },
        destination: { Text("Pushed View") },
        label: { Text("Tap me") }
      )
    
    0 讨论(0)
  • 2020-12-02 12:38

    You can also put it in the .background modifier:

    List {
        Text("Go to...")
            .background(NavigationLink("", destination: Text("Detail View")))
    }
    

    If you already have the background modifier on the Text, you can wrap the Text in a HStack and apply background to the HStack.

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

    there is no documentation yet, so you can use ScrollView for now

      NavigationView {
            ScrollView {
                ForEach(0...100){ x in
                    NavigationButton(destination: Text("ss")) {
                        HStack {
                              Text(String(x))
                              Spacer()
                            }
                            .padding()
                            .background(Color.white)
                            .shadow(radius:1,y:1)
                    }
                 }
                 .frame(width: UIScreen.main.bounds.width - 32)
                 .padding()
            }
        }
    

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

    Works well for me!

    import SwiftUI
    
    struct LandmarkList: View {
        var body: some View {
            NavigationView {
                List(landmarkData) { landmark in
                    LandmarkRow(landmark: landmark)
                    NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
                        EmptyView()
                    }
                }
                .navigationBarTitle(Text("Landmarks"))
            }
        }
    }
    
    struct LandmarkList_Previews: PreviewProvider {
        static var previews: some View {
            ForEach(["iPhone SE", "iPhone 11 Pro Max"], id: \.self) { deviceName in
                LandmarkList()
                    .previewDevice(PreviewDevice(rawValue: deviceName))
                    .previewDisplayName(deviceName)
            }
        }
    }
    
    

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

    What you can do, if you are using list, is setting the navigationlink to hidden and its frame width to zero.

        HStack{
                Button(action: {self.statusShow = 1}, label: {
                    Image(systemName: "info.circle")
                })
                NavigationLink(destination: StimulatorSettingView(),
                               tag: 1,
                               selection: self.$statusShow){
                                EmptyView()
    
                }.hidden().frame(width: 0)
            }
    

    This worked for me.

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