2016-10-21 22 views
0

iPhoneアプリは現在IOS 8/9/10をサポートしています。カスタムUITableViewCellのアクセシビリティ上の声をサポートするのが難しいです。私は以下のSOの記事を読んだが、提案のどれも働いていない。個々のコンポーネントにアクセス可能にしたい。私にとっては残念ながらカスタムUITableViewCellのボイスオーバーアクセシビリティを設定できません

  1. Custom UITableview cell accessibility not working correctly
  2. Custom UITableViewCell trouble with UIAccessibility elements
  3. Accessibility in custom drawn UITableViewCell
  4. https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/iPhoneAccessibility/Making_Application_Accessible/Making_Application_Accessible.html#//apple_ref/doc/uid/TP40008785-CH102-SW10
  5. http://useyourloaf.com/blog/voiceover-accessibility/

、セルは、アクセシビリティインスペクタによって検出されていません。テーブルビューのセル内の個々の要素を取り上げるためのアクセシビリティを声高にする方法はありますか?デバイスとシミュレータの両方でこの問題をデバッグすると、XCodeがisAccessibleElement関数を呼び出すことがわかりました。関数がNOを返した場合、残りのメソッドはスキップされます。私はXcodeでIOS 9.3をテストしています。

私のカスタムテーブルビューセルは、以下のようにラベルとスイッチで構成されています。スイッチは、カスタムアクセサリビューに追加されている間

Custom UITableView Cell with a label and a switch

ラベルは、コンテンツビューに追加されます。

インタフェース定義は、実装ブロックはあなたが説明したパターンから、私はあなたがしたいと思う理由はわからないんだけど、全ての

@interface MyCustomTableViewCell() 

    @property (nonatomic, strong) UIView *customAccessoryView; 
    @property (nonatomic, strong) NSString *alertTextString; 
    @property (nonatomic, strong) NSMutableArray* accessibleElements; 
@end 

@implementation MyCustomTableViewCell 


    - (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier 
    { 
     if(self = [super initWithStyle:UITableViewCellStyleDefault   
      reuseIdentifier:reuseIdentifier]) { 
      [self configureTableCell]; 
     } 
     return self; 
    } 


    - (void)configureTableCell 
    { 
     if (!_accessibleElements) { 
      _accessibleElements = [[NSMutableArray alloc] init]; 
     } 


    //Alert label 
    self.alertLabel = [[self class] makeAlertLabel]; 
    [self.contentView setIsAccessibilityElement:YES]; 
// 
    [self.contentView addSubview:self.alertLabel]; 

    // Custom AccessoryView for easy styling. 
    self.customAccessoryView = [[UIView alloc] initWithFrame:CGRectZero]; 
    [self.customAccessoryView setIsAccessibilityElement:YES]; 
    [self.contentView addSubview:self.customAccessoryView]; 

//switch 
    self.switch = [[BAUISwitch alloc] initWithFrame:CGRectZero]; 
    [self.switch addTarget:self action:@selector(switchWasFlipped:) forControlEvents:UIControlEventValueChanged]; 
[self.switch setIsAccessibilityElement:YES]; 
[self.switch setAccessibilityTraits:UIAccessibilityTraitButton]; 
[self.switch setAccessibilityLabel:@""]; 
[self.switch setAccessibilityHint:@""]; 
self.switch.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
[self.customAccessoryView addSubview:self.switch]; 
} 

+ (UILabel *)makeAlertLabel 
{ 
    UILabel *alertLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    alertLabel.backgroundColor = [UIColor clearColor]; 
    alertLabel.HTMLText = @""; 
    alertLabel.numberOfLines = 0; 
    alertLabel.lineBreakMode = LINE_BREAK_WORD_WRAP 
    [alertLabel setIsAccessibilityElement:YES]; 
    return alertLabel; 
} 

-(void)setAlertHTMLText:(NSString*)title{ 
    _alertTextString = [NSString stringWithString:title]; 
    [self.alertLabel setText:_alertTextString]; 
} 


- (BOOL)isAccessibilityElement { 
    return NO; 
} 

    // The view encapsulates the following elements for the purposes of  
// accessibility. 
-(NSArray*) accessibleElements { 
    if (_accessibleElements && [_accessibleElements count] > 0) { 
     [_accessibleElements removeAllObjects]; 
    } 
    // Fetch a new copy as the values may have changed. 
    _accessibleElements = [[NSMutableArray alloc] init]; 
    UIAccessibilityElement* alertLabelElement = 
    [[UIAccessibilityElement alloc] initWithAccessibilityContainer:self]; 
    //alertLabelElement.accessibilityFrame = [self convertRect:self.contentView.frame toView:nil]; 
    alertLabelElement.accessibilityLabel = _alertTextString; 
    alertLabelElement.accessibilityTraits = UIAccessibilityTraitStaticText; 
    [_accessibleElements addObject:alertLabelElement]; 


    UIAccessibilityElement* switchElement = 
     [[UIAccessibilityElement alloc] initWithAccessibilityContainer:self]; 
    // switchElement.accessibilityFrame = [self convertRect:self.customAccessoryView.frame toView:nil]; 
    switchElement.accessibilityTraits = UIAccessibilityTraitButton; 
    // If you want custom values, just override it in the invoking function. 
    NSMutableString* accessibilityString = 
     [NSMutableString stringWithString:self.accessibilityPrefix]; 
    [accessibilityString appendString:@" Switch "]; 
    if (self.switchh.isOn) { 
     [accessibilityString appendString:@"On"]; 
    } else { 
     [accessibilityString appendString:@"Off"]; 
    } 
    switchElement.accessibilityLabel = [accessibilityString copy]; 

    [_accessibleElements addObject:switchElement]; 
    } 
    return _accessibleElements; 
} 

// In case accessibleElements is not initialized. 
- (void) initializeAccessibleElements { 
    _accessibleElements = [self accessibleElements]; 
} 

- (NSInteger)accessibilityElementCount 
{ 
    return [_accessibleElements count] 
} 

- (id)accessibilityElementAtIndex:(NSInteger)index 
    { 
    [self initializeAccessibleElements]; 
    return [_accessibleElements objectAtIndex:index]; 
} 

- (NSInteger)indexOfAccessibilityElement:(id)element 
{ 
    [self initializeAccessibleElements]; 
    return [_accessibleElements indexOfObject:element]; 
} 
@end 
+0

は、uは、テーブルビューとセルのアクセシビリティを設定しましたか。それに関連するコードを表示します。 –

+0

また、あなたがあなただけaccessibleElementsプロパティに配列を渡すことができ、accessibleElementsをオーバーライドする必要がいけない。 –

+0

@TejaNandamuriこれはコンテナビューであるため、 "isAccessibleElement"のテーブルビューコンテナセルのアクセシビリティを "いいえ"に設定しました。そのコードブロックはコードブロックで提供されます。あなたの2番目のコメントには、ユーザーがスイッチを動かせるようになるたびにaccessibleElementsを計算する必要があります。それが起こった場合、私はその価値を反映しています。 – Kartik

答えて

4

まず以下の通りである

@interface MyCustomTableViewCell : UITableViewCell 

///Designated initializer 
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier; 


///Property that determines if the switch displayed in the cell is ON or OFF. 
@property (nonatomic, assign) BOOL switchIsOn; 

///The label to be displayed for the alert 
@property (nonatomic, strong) UILabel *alertLabel; 

@property (nonatomic, strong) UISwitch *switch; 

#pragma mark - Accessibility 
// Used for setting up accessibility values. This is used to generate accessibility labels of 
// individual elements. 
@property (nonatomic, strong) NSString* accessibilityPrefix; 

-(void)setAlertHTMLText:(NSString*)title; 


@end 

以下に示します。セル内のさまざまな要素を区別します。一般に、Appleはすべてのセルに単一のアクセシビリティ要素を保持します。ラベルとスイッチが設定されているセルの予想されるiOS VOの動作を確認するには、[設定アプリ]をご利用ください。

セルを処理する最良の方法は、セルに個々の要素を含めることだと思われる場合は、にアクセシビリティラベルが付いていない場合は、セルのデフォルト動作となるのが実際にはです。したがって、私はあなたのコードを以下のように変更し、iOSデバイス(9.3を実行しています)で実行しました。

あなたはいくつか気づくでしょう。

  1. すべてのカスタムaccessibilityElementsコードを削除しました。それは必要ない。
  2. UITableViewCellサブクラス自体のisAccessibilityElementのオーバーライドを削除しました。私たちはデフォルトの動作をしたい。
  3. コンテンツビューをaccessibilityElementとして設定したことをコメントアウトしました。ツリービルダーが要素を内部で探すようにNOにします。
  4. 上記と同じ理由で、customAccessoryViewのisAccessibilityElementをNOに設定しました。一般的に、NOは「木を見下ろし続ける」と言い、YESは「ここでやめてください」と答えています。アクセシビリティに関する限り、これは私の葉です。「

私はこれが便利であると思います。もう一度、私は本当にアクセシビリティを設計する際にAppleのVOパターンを模倣することをお勧めしますが。私はそれはあなたがあなたのアプリケーションがアクセス可能であることを確認作っていることをすごいと思う!

#import "MyCustomTableViewCell.h" 
 

 
@interface MyCustomTableViewCell() 
 

 
@property (nonatomic, strong) UIView *customAccessoryView; 
 
@property (nonatomic, strong) NSString *alertTextString; 
 
@property (nonatomic, strong) NSMutableArray* accessibleElements; 
 
@end 
 

 
@implementation MyCustomTableViewCell 
 

 
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
 
{ 
 
    if(self = [super initWithStyle:UITableViewCellStyleDefault 
 
        reuseIdentifier:reuseIdentifier]) { 
 
     [self configureTableCell]; 
 
    } 
 
    return self; 
 
} 
 

 
// just added this here to get the cell to lay out for myself 
 
- (void)layoutSubviews { 
 
    [super layoutSubviews]; 
 
    
 
    const CGFloat margin = 8; 
 
    
 
    CGRect b = self.bounds; 
 
    
 
    CGSize labelSize = [self.alertLabel sizeThatFits:b.size]; 
 
    CGFloat maxX = CGRectGetMaxX(b); 
 
    self.alertLabel.frame = CGRectMake(margin, margin, labelSize.width, labelSize.height); 
 
    
 
    CGSize switchSize = [self.mySwitch sizeThatFits:b.size]; 
 
    self.customAccessoryView.frame = CGRectMake(maxX - switchSize.width - margin * 2, b.origin.y + margin, switchSize.width + margin * 2, switchSize.height); 
 
    self.mySwitch.frame = CGRectMake(margin, 0, switchSize.width, switchSize.height); 
 
} 
 

 

 
- (void)configureTableCell 
 
{ 
 
    //Alert label 
 
    self.alertLabel = [[self class] makeAlertLabel]; 
 
    //[self.contentView setIsAccessibilityElement:YES]; 
 
    // 
 
    [self.contentView addSubview:self.alertLabel]; 
 
    
 
    // Custom AccessoryView for easy styling. 
 
    self.customAccessoryView = [[UIView alloc] initWithFrame:CGRectZero]; 
 
    [self.customAccessoryView setIsAccessibilityElement:NO]; // Setting this to NO tells the the hierarchy builder to look inside 
 
    [self.contentView addSubview:self.customAccessoryView]; 
 
    self.customAccessoryView.backgroundColor = [UIColor purpleColor]; 
 
    
 
    //switch 
 
    self.mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
 
    //[self.mySwitch addTarget:self action:@selector(switchWasFlipped:) forControlEvents:UIControlEventValueChanged]; 
 
    [self.mySwitch setIsAccessibilityElement:YES]; // This is default behavior 
 
    [self.mySwitch setAccessibilityTraits:UIAccessibilityTraitButton]; // No tsure why this is here 
 
    [self.mySwitch setAccessibilityLabel:@"my swich"]; 
 
    [self.mySwitch setAccessibilityHint:@"Tap to do something."]; 
 
    self.mySwitch.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
 
    [self.customAccessoryView addSubview:self.mySwitch]; 
 
} 
 

 
+ (UILabel *)makeAlertLabel 
 
{ 
 
    UILabel *alertLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
 
    alertLabel.backgroundColor = [UIColor clearColor]; 
 
    alertLabel.text = @""; 
 
    alertLabel.numberOfLines = 0; 
 
    [alertLabel setIsAccessibilityElement:YES]; 
 
    return alertLabel; 
 
} 
 

 
-(void)setAlertHTMLText:(NSString*)title{ 
 
    _alertTextString = [NSString stringWithString:title]; 
 
    [self.alertLabel setText:_alertTextString]; 
 
} 
 

 
@end

+1

こんにちは、ソマー、お詫び申し上げます遅延のために。あなたの提案はすばらしく機能しました。 – Kartik

関連する問題