简体   繁体   中英

NavigationLink on segmented style Picker has no effect

I have the following structure:

Picker("", selection: $accountIndex) {
    ForEach(0..<accounts.count) { index in
        NavigationLink(destination: VStack { Text("Hello") }, 
                       label: { Text(self.accounts[index].name) })
    }
}
.pickerStyle(SegmentedPickerStyle())

but the Navigation does not work because the tap gesture only modifies the selection of the segmented picker. Is there a workaround for this?

Now you might ask why I want to do this as it seems weird that a segmented picker should navigate anywhere - but I am working on an iPad app, and in that case I see the segmented control on the left main column, and I wanna see something based on segmented picker selection on the right detail column.

Buttons and NavigationLinks don't receive the tap gesture when within a picker Content .

Something you can try is make the Links an HStack

import SwiftUI

struct NavPicker: View {
    @FetchRequest(entity: YourModel.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \YourModel.id, ascending: false)]) var objects: FetchedResults<YourModel>

    var body: some View {
        NavigationView{
        VStack{
            HStack{
                ForEach(0..<objects.count) { index in
                    NavigationLink(destination: VStack { Text("Hello \(index)") },
                            label: { Text(self.objects[index].name ?? "No name") })
                        .background(Color.white.opacity(0.5))
                        .padding(1)
                 }

            }
        }.background(Color.gray)

        }.navigationViewStyle(StackNavigationViewStyle())

    }
}

struct NavPicker_Previews: PreviewProvider {
    static var previews: some View {
        NavPicker()
    }
}

This solution doesn't work as well with the iPads DoubleColumnNavigationViewStyle() .

There seems to be a bug with that I recently had an app that bounced back for giving a blank screen on an iPad and it was solved by setting the Style.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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