如何使UISearchBar的书签图标可访问?

时间:2022-11-25 16:53:42

I'm trying to improve Voice Over support in an app. I have a UISearchBar. I've set the showBookMarks property to YES. And I've set a custom image with:

我正在尝试在应用中改进Voice Over支持。我有一个UISearchBar。我已将showBookMarks属性设置为YES。我已经设置了一个自定义图像:

[searchbar setImage:icon forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];

The problem I have is that with Voice Over turned on, there is no way to activate the bookmarks icon. If I enter text, the "clear" icon appears and it can be selected and activated as expected. But once the text is cleared and my bookmarks icon appears, it can't be selected. No matter where I tap, the Voice Over rectangle always surrounds the entire search bar, including the icons.

我遇到的问题是,启用Voice Over后,无法激活书签图标。如果我输入文本,则会出现“清除”图标,可以按预期选择并激活它。但是一旦文本被清除并且我的书签图标出现,就无法选择它。无论我在哪里点击,Voice Over矩形始终围绕整个搜索栏,包括图标。

I've tried setting the accessibilityLabel and the accessibilityTraits properties on the UIImage for the icon. I've set the UIImage's isAccessibilityElement property to YES. Nothing seems to work.

我已经尝试在UIImage上为图标设置accessibilityLabel和accessibilityTraits属性。我已将UIImage的isAccessibilityElement属性设置为YES。似乎没什么用。

Does anyone know what steps are required to make the bookmarks icon in a UISearchBar accessible?

有谁知道使UISearchBar中的书签图标可访问需要哪些步骤?

Here's the complete code for the search bar:

这是搜索栏的完整代码:

UISearchBar *bar = [[UISearchBar alloc] init];
[bar sizeToFit];
bar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
bar.placeholder = @"Search listed items";
bar.autocapitalizationType = UITextAutocapitalizationTypeNone;
bar.autocorrectionType = UITextAutocorrectionTypeNo;
bar.keyboardType = UIKeyboardTypeDefault;
bar.showsBookmarkButton = YES;
bar.text = @"";

UIImage *icon = [UIImage imageNamed:@"bookmarks.png"];
icon.accessibilityLabel = @"Bookmarks";
icon.accessibilityTraits = UIAccessibilityTraitButton;
icon.isAccessibilityElement = YES;
[bar setImage:icon forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
[bar setPositionAdjustment:UIOffsetMake(-1, -1) forSearchBarIcon:UISearchBarIconBookmark];

Additional Info:

I should mention that the search bar is used with a UITableViewController which is connected with a UISearchDisplayController. The search bar is being set as the table view's headerView.

我应该提一下,搜索栏与UITableViewController一起使用,该UITableViewController与UISearchDisplayController连接。搜索栏被设置为表视图的headerView。

And all of this is done in code. No storyboards or xibs are involved.

所有这些都是在代码中完成的。不涉及故事板或xib。

2 个解决方案

#1


4  

After a bunch of debugging, I have found the answer. In order for Voice Over to recognize a custom icon for the bookmarks icon of a UISearchBar, the icon must be exactly the correct size.

经过一堆调试后,我找到了答案。为了使Voice Over能够识别UISearchBar的书签图标的自定义图标,该图标必须具有正确的大小。

The images used must be 40x29px (80x58px for the retina version). Any other size and the icons don't get recognized by Voice Over.

使用的图像必须是40x29px(视网膜版本为80x58px)。 Voice Over无法识别任何其他尺寸和图标。

My images were 38x31px (and 68x62px).

我的图像是38x31px(和68x62px)。

The code I posted in my original question is all correct. Once the images were updated in size, everything started to work as expected. The only change in code was the removal of the call to setPositionAdjustment:forSearchBarIcon: which wasn't needed anymore.

我在原始问题中发布的代码都是正确的。一旦图像大小更新,一切都开始按预期工作。代码中唯一的变化就是删除了对setPositionAdjustment的调用:forSearchBarIcon:不再需要了。

I can't imaging why such a tiny difference in icon size would be the difference between Voice Over working with custom icons and not working. But there it is. I hope this helps someone in the future.

我无法想象为什么图标大小的这么小的差异将是Voice Over使用自定义图标和不工作之间的区别。但就是这样。我希望这能帮助将来的某个人。

#2


1  

I've run the code in your question, and the bookmarks button appears to be accessible via VoiceOver.

我在您的问题中运行了代码,并且书签按钮似乎可以通过VoiceOver访问。

如何使UISearchBar的书签图标可访问?

Could there be something else that's interfering with your interface? Any invisible views that might find their way on top of the bookmarks button? Are you setting a custom clear button? Any other details you can provide?

可能还有其他东西会干扰您的界面吗?任何不可见的视图可能会在书签按钮上找到它们的方式?你在设置自定义清除按钮吗?您可以提供的任何其他细节?

However, it's true that it's not using the accessibilityLabel that you're passing it; it just uses "Button", which sucks. I'd say it's probably a bug in UIKit – you should file a bug.

但是,它确实没有使用你传递它的accessibilityLabel;它只是使用“按钮”,这很糟糕。我说它可能是UIKit中的一个错误 - 你应该提交一个bug。

#1


4  

After a bunch of debugging, I have found the answer. In order for Voice Over to recognize a custom icon for the bookmarks icon of a UISearchBar, the icon must be exactly the correct size.

经过一堆调试后,我找到了答案。为了使Voice Over能够识别UISearchBar的书签图标的自定义图标,该图标必须具有正确的大小。

The images used must be 40x29px (80x58px for the retina version). Any other size and the icons don't get recognized by Voice Over.

使用的图像必须是40x29px(视网膜版本为80x58px)。 Voice Over无法识别任何其他尺寸和图标。

My images were 38x31px (and 68x62px).

我的图像是38x31px(和68x62px)。

The code I posted in my original question is all correct. Once the images were updated in size, everything started to work as expected. The only change in code was the removal of the call to setPositionAdjustment:forSearchBarIcon: which wasn't needed anymore.

我在原始问题中发布的代码都是正确的。一旦图像大小更新,一切都开始按预期工作。代码中唯一的变化就是删除了对setPositionAdjustment的调用:forSearchBarIcon:不再需要了。

I can't imaging why such a tiny difference in icon size would be the difference between Voice Over working with custom icons and not working. But there it is. I hope this helps someone in the future.

我无法想象为什么图标大小的这么小的差异将是Voice Over使用自定义图标和不工作之间的区别。但就是这样。我希望这能帮助将来的某个人。

#2


1  

I've run the code in your question, and the bookmarks button appears to be accessible via VoiceOver.

我在您的问题中运行了代码,并且书签按钮似乎可以通过VoiceOver访问。

如何使UISearchBar的书签图标可访问?

Could there be something else that's interfering with your interface? Any invisible views that might find their way on top of the bookmarks button? Are you setting a custom clear button? Any other details you can provide?

可能还有其他东西会干扰您的界面吗?任何不可见的视图可能会在书签按钮上找到它们的方式?你在设置自定义清除按钮吗?您可以提供的任何其他细节?

However, it's true that it's not using the accessibilityLabel that you're passing it; it just uses "Button", which sucks. I'd say it's probably a bug in UIKit – you should file a bug.

但是,它确实没有使用你传递它的accessibilityLabel;它只是使用“按钮”,这很糟糕。我说它可能是UIKit中的一个错误 - 你应该提交一个bug。