简体   繁体   English

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

[英]How to make a UISearchBar's bookmarks icon accessible?

I'm trying to improve Voice Over support in an app. 我正在尝试在应用中改善Voice Over支持。 I have a UISearchBar . 我有一个UISearchBar I've set the showBookMarks property to YES . 我已将showBookMarks属性设置为YES And I've set a custom image with: 我已经设置了一个自定义图像:

[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. 我遇到的问题是,启用Voice Over后,无法激活书签图标。 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矩形始终围绕整个搜索栏,包括图标。

I've tried setting the accessibilityLabel and the accessibilityTraits properties on the UIImage for the icon. 我已经尝试在UIImage为图标设置accessibilityLabelaccessibilityTraits属性。 I've set the UIImage 's isAccessibilityElement property to YES . 我已将UIImageisAccessibilityElement属性设置为YES Nothing seems to work. 似乎没什么用。

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 . 我应该提到搜索栏与UITableViewController一起使用,该UITableViewControllerUISearchDisplayController连接。 The search bar is being set as the table view's headerView . 搜索栏被设置为表视图的headerView

And all of this is done in code. 所有这些都是在代码中完成的。 No storyboards or xibs are involved. 不涉及故事板或xib。

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). 使用的图像必须是40x29px(视网膜版本为80x58px)。 Any other size and the icons don't get recognized by Voice Over. 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:的调用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. 我无法想象为什么图标大小的这么小的差异将是Voice Over使用自定义图标和不工作之间的区别。 But there it is. 但它确实如此。 I hope this helps someone in the future. 我希望这可以帮助将来的某个人。

I've run the code in your question, and the bookmarks button appears to be accessible via VoiceOver. 我在您的问题中运行了代码,并且书签按钮似乎可以通过VoiceOver访问。

在此输入图像描述

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; 但是,它确实没有使用你传递它的accessibilityLabel ; it just uses "Button", which sucks. 它只是使用“按钮”,这很糟糕。 I'd say it's probably a bug in UIKit – you should file a bug . 我说它可能是UIKit中的一个错误 - 你应该提交一个bug

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM