Custom UITableViewCell accessibility

前端 未结 4 1921
别那么骄傲
别那么骄傲 2021-02-13 23:09

How can you set the accessibility of subviews of UITableViewCells individually.
It should not be accessible as complete cell?

相关标签:
4条回答
  • 2021-02-13 23:43

    Contibuting to answer of @ma11hew28, here is another way to do that saves some lines of code, in Swift 5:

    class ServiceTableViewCell: UITableViewCell {
        override func awakeFromNib() {
            super.awakeFromNib()
            self.selectionStyle = .none
            self.isAccessibilityElement = false //not allowing accessibility for the cell itself
            self.accessibilityElements = [mySubview1!, mySubview2!, mySubview3!] // but allowing accessibility in the cell's subviews
        }
    }
    
    0 讨论(0)
  • 2021-02-13 23:53

    If your tableview cell consists of text only, enabling accessibility for the cell will do and it will read out the whole cell. If you have other objects and buttons, it is recommended to use a subclass of UITableViewCell and override the -accessibilityElements method to return all your accessible elements.

    Some code:

    #import "CustomCell.h"
    
    @implementation CustomCell
    
    - (void)awakeFromNib
    {
        [super awakeFromNib];
        self.accessibilityElements = @[self.view1, self.label, self.imageView];
    }
    

    Following post may help:

    http://cocoacaffeine.wordpress.com/2013/11/29/little-tricks-of-accessibility/

    0 讨论(0)
  • 2021-02-13 23:53

    Make the cell inaccessible and your subviews accessible in tableView(_:cellForRowAt:).

    isAccessibilityElement = false
    mySubview1.isAccessibilityElement = true
    mySubview2.isAccessibilityElement = true
    mySubview3.isAccessibilityElement = true
    

    I got stuck on this too because I (trying to be efficient) put the above in my custom cell's init(style:reuseIdentifier:) method. But, that didn't work, probably because UITableView resets everything after initializing each of its cells.

    Related: Accessibility Programming Guide for iOS: Enhance the Accessibility of Table Views

    0 讨论(0)
  • 2021-02-13 23:58

    I tried every solution proposed here, but none of those solved my issue.

    I was able to hide my cell from accessibility and highlight only the elements from my UITableViewCell by setting the cell's contentView.accessibilityElementsHidden to true and also setting my UI items isAccessibilityElement to true.

    contentView.accessibilityElementsHidden = true
    
    UIElement.isAccessibilityElement = true
    
    0 讨论(0)
提交回复
热议问题