listswiftuiswiftui-navigationlink

SwiftUI: How to add a value near the disclosure indicator of a NavigationLink in a list


I am trying to build something like below. The only missing piece is the value at the end, near the disclosure indicator. Any idea how to add that value using SwiftUI?

I have a list and the list element is a NavigationLink. I am passing a Binding var to the destination where I select from multiple options the value of that var.

I want to show 'None' in case no selection is done at that moment or the selected value when a selection is done in the destination view.

enter image description here


Solution

  • First of all, you have to include the content inside a Form or a List.

    1. When using a Form with a TextField:
    Form {
        HStack {
            Text("Label")
            
            TextField("Label", text: $label)
                .multilineTextAlignment(.trailing)
        }
    }
    
    1. When using a form with a NavigationLink:
    Form {
        NavigationLink {
            DestinationView()
        } label: {
            Text("Category")
                .badge("None") // this will show the secondary text near the disclosure indicator
        }
    }
    

    And then you can play form here and display as badge the content you want (a selection made in the destionation).