xmartlabs / PagerTabStripView

🚀 Elegant Pager View fully written in pure SwiftUI.
MIT License
774 stars 88 forks source link

'.isSelected' accessibility trait on the selected NavBarItem #98

Closed marco-mattei-mo closed 1 year ago

marco-mattei-mo commented 1 year ago

Hello, I am using this package in an app and I realised that the active NavBarItem isn't recognized as selected by VoiceOver. It would be nice to add the .isSelected accessibility trait to the selected NavBarItem.

I would propose something like this on the NavBarItem button:

.accessibilityAddTraits(id == selection ? .isSelected : [])

I don't think it's possible to do it from outside the package, I tried dynamically adding the trait to the pagerTabItem according to the $selection binding, like this:

.pagerTabItem {
    Text("Any title")
     .frame(maxWidth: .infinity, maxHeight: .infinity)
     .accessibilityAddTraits(currentTab == 0 ? .isSelected: [])
}

But I think the view set in here is not redrawn, the PagerTabStripView will only draw the one received on initialisation.

mlorenze commented 1 year ago

Hello, I am using this package in an app and I realised that the active NavBarItem isn't recognized as selected by VoiceOver. It would be nice to add the .isSelected accessibility trait to the selected NavBarItem.

I would propose something like this on the NavBarItem button:

.accessibilityAddTraits(id == selection ? .isSelected : [])

I don't think it's possible to do it from outside the package, I tried dynamically adding the trait to the pagerTabItem according to the $selection binding, like this:

.pagerTabItem {
    Text("Any title")
     .frame(maxWidth: .infinity, maxHeight: .infinity)
     .accessibilityAddTraits(currentTab == 0 ? .isSelected: [])
}

But I think the view set in here is not redrawn, the PagerTabStripView will only draw the one received on initialisation.

Hi! Thanks to use our framework! Regarding with your request: With the latest updates you can setup in each pagerTabItem the corresponding tag. So now is possible to add the selected trait from outside the package (the same way you handle it in the example you gave).

Here is the part of the Readme document where you can see how to setup the tag:

To specify the initial selected page you can pass the selection init parameter (for it to work properly this value have to be equal to some tag value of the tab items).

struct MyPagerView: View {

    @State var selection = 1

    var body: some View {
        PagerTabStripView(selection: $selection) {
            MyFirstView()
                .pagerTabItem(tag: 1) {
                    TitleNavBarItem(title: "Tab 1")
                }
            ...
            ..
            .
        }
    }
}