SwiftUI NavigationButton without the disclosure in

2020-01-30 04:19发布

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?

    NavigationView {
        List {
            NavigationButton(destination: DetailView()) {
                ListItem()
            }
        }
        .navigationBarTitle(Text("Some title"))
    }

On a UITableViewCell you set Accessory to None but how do I do that in SwiftUI?

标签: swiftui
10条回答
Juvenile、少年°
2楼-- · 2020-01-30 04:44

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.

查看更多
唯我独甜
3楼-- · 2020-01-30 04:46

This helps to push and pass the model to the next navigation view controller.

struct ContentView : View {
    @State var model = PostListViewModel()

    var body: some View {
        NavigationView {
            List(model.post) { post in
                ListCell(listData: post)
                }.navigationBarTitle(Text("My Post"))
        }
    }

}

struct ListCell: View {
    var listData: Post
    var body: some View {
        return NavigationButton(destination: DetailContentView(post: listData)) {
            HStack {
                ImageRow(model: listData) // Get image
                VStack(alignment: .leading) {
                    Text(listData.login).font(.headline).lineLimit(nil)
                    Text(listData.url).font(.subheadline).lineLimit(nil)
                    }.padding(.leading, 10)
                }.padding(.init(top: 5, leading: 0, bottom: 5, trailing: 0))
        }
    }
}
查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-01-30 04:46

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()
     }
   }
}
查看更多
Melony?
5楼-- · 2020-01-30 04:48

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()
                    })
                }
            }
        }
    }
}
查看更多
Lonely孤独者°
6楼-- · 2020-01-30 04:48

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)
查看更多
看我几分像从前
7楼-- · 2020-01-30 04:57

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)
    }
}
查看更多
登录 后发表回答