辅助功能看不到 SWTableViewCell 的实用程序按钮
Accessibility doesn't see SWTableViewCell's utility buttons
我正在使用 SWTableviewCell,我可以向左滑动并显示带有 XCTest 和辅助功能的实用程序按钮,但我找不到这些元素。使用辅助功能检查器仔细检查。
由于 SWTableViewCell 中的实用程序按钮是常规的 UIButton,因此它应该开箱即用。还尝试设置按钮的辅助功能标签。
如有任何帮助,我们将不胜感激。谢谢!
更新:
这是我的代码:
// This works
app = XCUIApplication()
let firstCell = app.tables.cells.elementBoundByIndex(0)
firstCell.swipeLeft()
// Following 2 TODO's doesn't work
// TODO: tap More button
firstCell.buttons["More"].tap()
XCTAssertEqual(app.sheets.count, 1, "'More' action sheet should be present")
// TODO: assert sheet doesn't contain Lock/Unlock button
let sheet = app.sheets.element
let lockButton = sheet.buttons["Lock"]
sheet.buttons["Cancel"].tap()
最终为我的 UITableViewCell
子类覆盖了 UIAccessibilityContainer
方法,如下所示:
- (NSArray *)accessibilityElements {
if (_accessibilityElements == nil) {
_accessibilityElements = [[NSMutableArray alloc] initWithCapacity: 2];
[_accessibilityElements addObject: self.miscLabel];
[_accessibilityElements addObject: self.titleLabel];
}
return _accessibilityElements;
}
- (nullable id)accessibilityElementAtIndex:(NSInteger)index {
return _accessibilityElements[index];
}
- (NSInteger)indexOfAccessibilityElement:(id)element {
return [_accessibilityElements indexOfObject: element];
}
注意 _accessibilityElements
是我的单元格的实例变量:
@interface MyCell : SWTableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *miscLabel;
...
@property (strong, nonatomic) NSMutableArray *accessibilityElements;
@end
然后,实施 SWTableViewCellDelegate
方法来向辅助功能添加和删除实用程序按钮:
- (void)swipeableTableViewCellDidEndScrolling:(MyCell *)cell {
NSMutableArray *utilityButtons = cell.accessibilityElements;
if (cell.isUtilityButtonsHidden) {
[utilityButtons removeObjectsInArray: cell.rightUtilityButtons];
}
else {
[utilityButtons addObjectsFromArray: cell.rightUtilityButtons];
}
}
在 SWTableViewCell.h 中,在文件末尾添加以下代码行。
// Accessibility
@property (strong, nonatomic) NSMutableArray *accessibilityElements;
- (void)addElementForAccesiblilty:(id)element;
- (void)removeElementFromAccesiblilty:(id)element;
以及 SWTableViewCell.m
中的以下几行
#pragma mark Accessibilty
- (void)addElementForAccesiblilty:(id)element
{
if (![self.accessibilityElements containsObject:element]) {
[self.accessibilityElements addObject:element];
}
}
- (void)removeElementFromAccesiblilty:(id)element
{
[self.accessibilityElements removeObject:element];
}
- (NSArray *)accessibilityElements {
if (!_accessibilityElements) {
_accessibilityElements = [[NSMutableArray alloc] initWithCapacity: 0];
[self updateAccessibilityElementsForView:self];
}
return _accessibilityElements;
}
- (void)updateAccessibilityElementsForView:(UIView*)v
{
for (UIView *subView in v.subviews)
{
if ([subView isKindOfClass:[UILabel class]] || [subView isKindOfClass:[UIButton class]]) {
[_accessibilityElements addObject:subView];
}
[self updateAccessibilityElementsForView:subView];
}
}
- (nullable id)accessibilityElementAtIndex:(NSInteger)index {
id element = nil;
@synchronized(_accessibilityElements) {
element = _accessibilityElements[index];
}
return element;
}
- (NSInteger)indexOfAccessibilityElement:(id)element {
NSInteger index = NSNotFound;
@synchronized(_accessibilityElements) {
index = [_accessibilityElements indexOfObject: element];
}
return index;
}
- (void)updateAccessibleElementsForUtilityButtons {
switch (self.cellState) {
case kCellStateLeft:
{
if (self.isUtilityButtonsHidden) {
[self.accessibilityElements removeObjectsInArray: self.leftUtilityButtons];
}
else {
[self.accessibilityElements addObjectsFromArray: self.leftUtilityButtons];
}
break;
}
case kCellStateRight:
{
if (self.isUtilityButtonsHidden) {
[self.accessibilityElements removeObjectsInArray: self.rightUtilityButtons];
}
else {
[self.accessibilityElements addObjectsFromArray: self.rightUtilityButtons];
}
break;
}
default: {
[self.accessibilityElements removeObjectsInArray: self.rightUtilityButtons];
[self.accessibilityElements removeObjectsInArray: self.leftUtilityButtons];
break;
}
}
}
在scrollViewDidEndScrollingAnimation和scrollViewDidEndDecelerating中调用方法updateAccessibleElementsForUtilityButtons委托方法[=14] =]
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self updateCellState];
[self updateAccessibleElementsForUtilityButtons];
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCellDidEndScrolling:)]) {
[self.delegate swipeableTableViewCellDidEndScrolling:self];
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[self updateCellState];
[self updateAccessibleElementsForUtilityButtons];
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCellDidEndScrolling:)]) {
[self.delegate swipeableTableViewCellDidEndScrolling:self];
}
}
这会将所有按钮和标签添加为单元格中的可访问元素。但是如果你想 add/remove 任何元素用户遵循方法
- (void)addElementForAccesiblilty:(id)element;
- (void)removeElementFromAccesiblilty:(id)element;
我正在使用 SWTableviewCell,我可以向左滑动并显示带有 XCTest 和辅助功能的实用程序按钮,但我找不到这些元素。使用辅助功能检查器仔细检查。
由于 SWTableViewCell 中的实用程序按钮是常规的 UIButton,因此它应该开箱即用。还尝试设置按钮的辅助功能标签。
如有任何帮助,我们将不胜感激。谢谢!
更新: 这是我的代码:
// This works
app = XCUIApplication()
let firstCell = app.tables.cells.elementBoundByIndex(0)
firstCell.swipeLeft()
// Following 2 TODO's doesn't work
// TODO: tap More button
firstCell.buttons["More"].tap()
XCTAssertEqual(app.sheets.count, 1, "'More' action sheet should be present")
// TODO: assert sheet doesn't contain Lock/Unlock button
let sheet = app.sheets.element
let lockButton = sheet.buttons["Lock"]
sheet.buttons["Cancel"].tap()
最终为我的 UITableViewCell
子类覆盖了 UIAccessibilityContainer
方法,如下所示:
- (NSArray *)accessibilityElements {
if (_accessibilityElements == nil) {
_accessibilityElements = [[NSMutableArray alloc] initWithCapacity: 2];
[_accessibilityElements addObject: self.miscLabel];
[_accessibilityElements addObject: self.titleLabel];
}
return _accessibilityElements;
}
- (nullable id)accessibilityElementAtIndex:(NSInteger)index {
return _accessibilityElements[index];
}
- (NSInteger)indexOfAccessibilityElement:(id)element {
return [_accessibilityElements indexOfObject: element];
}
注意 _accessibilityElements
是我的单元格的实例变量:
@interface MyCell : SWTableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *miscLabel;
...
@property (strong, nonatomic) NSMutableArray *accessibilityElements;
@end
然后,实施 SWTableViewCellDelegate
方法来向辅助功能添加和删除实用程序按钮:
- (void)swipeableTableViewCellDidEndScrolling:(MyCell *)cell {
NSMutableArray *utilityButtons = cell.accessibilityElements;
if (cell.isUtilityButtonsHidden) {
[utilityButtons removeObjectsInArray: cell.rightUtilityButtons];
}
else {
[utilityButtons addObjectsFromArray: cell.rightUtilityButtons];
}
}
在 SWTableViewCell.h 中,在文件末尾添加以下代码行。
// Accessibility
@property (strong, nonatomic) NSMutableArray *accessibilityElements;
- (void)addElementForAccesiblilty:(id)element;
- (void)removeElementFromAccesiblilty:(id)element;
以及 SWTableViewCell.m
中的以下几行#pragma mark Accessibilty
- (void)addElementForAccesiblilty:(id)element
{
if (![self.accessibilityElements containsObject:element]) {
[self.accessibilityElements addObject:element];
}
}
- (void)removeElementFromAccesiblilty:(id)element
{
[self.accessibilityElements removeObject:element];
}
- (NSArray *)accessibilityElements {
if (!_accessibilityElements) {
_accessibilityElements = [[NSMutableArray alloc] initWithCapacity: 0];
[self updateAccessibilityElementsForView:self];
}
return _accessibilityElements;
}
- (void)updateAccessibilityElementsForView:(UIView*)v
{
for (UIView *subView in v.subviews)
{
if ([subView isKindOfClass:[UILabel class]] || [subView isKindOfClass:[UIButton class]]) {
[_accessibilityElements addObject:subView];
}
[self updateAccessibilityElementsForView:subView];
}
}
- (nullable id)accessibilityElementAtIndex:(NSInteger)index {
id element = nil;
@synchronized(_accessibilityElements) {
element = _accessibilityElements[index];
}
return element;
}
- (NSInteger)indexOfAccessibilityElement:(id)element {
NSInteger index = NSNotFound;
@synchronized(_accessibilityElements) {
index = [_accessibilityElements indexOfObject: element];
}
return index;
}
- (void)updateAccessibleElementsForUtilityButtons {
switch (self.cellState) {
case kCellStateLeft:
{
if (self.isUtilityButtonsHidden) {
[self.accessibilityElements removeObjectsInArray: self.leftUtilityButtons];
}
else {
[self.accessibilityElements addObjectsFromArray: self.leftUtilityButtons];
}
break;
}
case kCellStateRight:
{
if (self.isUtilityButtonsHidden) {
[self.accessibilityElements removeObjectsInArray: self.rightUtilityButtons];
}
else {
[self.accessibilityElements addObjectsFromArray: self.rightUtilityButtons];
}
break;
}
default: {
[self.accessibilityElements removeObjectsInArray: self.rightUtilityButtons];
[self.accessibilityElements removeObjectsInArray: self.leftUtilityButtons];
break;
}
}
}
在scrollViewDidEndScrollingAnimation和scrollViewDidEndDecelerating中调用方法updateAccessibleElementsForUtilityButtons委托方法[=14] =]
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self updateCellState];
[self updateAccessibleElementsForUtilityButtons];
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCellDidEndScrolling:)]) {
[self.delegate swipeableTableViewCellDidEndScrolling:self];
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[self updateCellState];
[self updateAccessibleElementsForUtilityButtons];
if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCellDidEndScrolling:)]) {
[self.delegate swipeableTableViewCellDidEndScrolling:self];
}
}
这会将所有按钮和标签添加为单元格中的可访问元素。但是如果你想 add/remove 任何元素用户遵循方法
- (void)addElementForAccesiblilty:(id)element;
- (void)removeElementFromAccesiblilty:(id)element;