Skip to content

Implement View.tag API #583

@Kyle-Ye

Description

@Kyle-Ye

Description

SwiftUI provides the tag(_:) modifier to set unique tag values on views. This is essential for differentiating among selectable views in containers like Picker and TabView. This API is currently missing from OpenSwiftUI.

API Reference

SwiftUI's tag modifier: https://developer.apple.com/documentation/swiftui/view/tag(_:)

Expected API

extension View {
    func tag<V>(_ tag: V) -> some View where V: Hashable
}

Use Case

The tag modifier is essential for:

  • Picker - Identifying selectable options
  • TabView - Differentiating between tabs
  • NavigationLink - Selection-based navigation
  • Any container that needs to identify and select among child views

Example Usage

struct FlavorPicker: View {
    enum Flavor: String, CaseIterable, Identifiable {
        case chocolate, vanilla, strawberry
        var id: Self { self }
    }
    
    @State private var selectedFlavor: Flavor = .chocolate
    
    var body: some View {
        Picker("Flavor", selection: $selectedFlavor) {
            ForEach(Flavor.allCases) { flavor in
                Text(flavor.rawValue)
                    .tag(flavor)
            }
        }
    }
}

Priority

High - Blocking functionality for Picker, TabView, and other selection-based containers

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedPR contribution is appreciated

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions