How to skip accessibility focus on one bar button item and keep focus on other bar button items of the toolbar

275 Views Asked by At

I have a toolbar , I'd like to skip the title button when accessibility is turned on.

I can skip accessibility for the whole tool bar using accessibilityElementsHidden.

but I just want to skip the accessibility focus of title bar button.

I tried disabling the title button's accessibility individually.

But it's not working.

So I set the AccessibilityElements property , it's skipping the title bar and reading out the DONE button on right side. But there's no individual focus on that DONE button at all. The focus is missing for individual bar button items when I use AccessibilityElements.

Update:

I have added an observer here

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

Here is the observer

 [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(beginEditingHandler:) name:UITextFieldTextDidBeginEditingNotification object:nil];

Here is how I'm managing the accessibility items

  - (void)beginEditingHandler:(NSNotification *)notification {
    UIResponder *responder = notification.object;
    IQToolbar *inputAccessoryView = responder.inputAccessoryView;
    if (inputAccessoryView) {
        if ([inputAccessoryView isKindOfClass:[IQToolbar class]]){
            NSMutableArray *arrAccessibilityItems = [[NSMutableArray alloc]init];
            if(inputAccessoryView.previousBarButton){
                if(!inputAccessoryView.previousBarButton)
                [arrAccessibilityItems addObject:inputAccessoryView.previousBarButton];
            }
            if(inputAccessoryView.nextBarButton){
                if(!inputAccessoryView.nextBarButton.isHidden)
                [arrAccessibilityItems addObject:inputAccessoryView.nextBarButton];
            }
            if(inputAccessoryView.titleBarButton){
                IQTitleBarButtonItem *titleBtn= inputAccessoryView.titleBarButton;
                if(titleBtn.title.length>0){
                    [arrAccessibilityItems addObject:inputAccessoryView.titleBarButton];
                }
            }
            if(inputAccessoryView.doneBarButton){
                [arrAccessibilityItems addObject:inputAccessoryView.nextBarButton];
            }
            [inputAccessoryView setAccessibilityElements:arrAccessibilityItems];

        }
    }
}

The problem is this read out as whole tool bar rather than a bar item by bar item.

1

There are 1 best solutions below

2
Eugene Dudnyk On

IQToolbar sets up a title button as follows:

    @objc open var titleBarButton: IQTitleBarButtonItem {
        get {
            if privateTitleBarButton == nil {
                privateTitleBarButton = IQTitleBarButtonItem(title: nil)
                privateTitleBarButton?.accessibilityLabel = "Title"
                privateTitleBarButton?.accessibilityIdentifier = privateTitleBarButton?.accessibilityLabel
            }
            return privateTitleBarButton!
        }

        set (newValue) {
            privateTitleBarButton = newValue
        }
    }

You'll have to subclass IQToolbar and override titleBarButton as follows:

open class MyToolbar: IQToolbar {
    open var titleBarButton: IQTitleBarButtonItem {
        get {
            let titleBarButton = super.titleBarButton
            titleBarButton.customView.subviews.first?.isAccessibilityElement = false
            return titleBarButton
        }
        set (newValue) {
            super.titleBarButton = newValue
        }
    }
}