Достопримечательности tvOS с дополнительными представлениями в представлении коллекции - PullRequest
0 голосов
/ 17 сентября 2018

Я пытаюсь скопировать поток для Landmark Accessibility из приложения Apple Movie. Я попытался использовать табличное представление с настраиваемым заголовком и стандартным представлением заголовка, в котором у моих ячеек есть представление сбора внутри, а также представление сбора с дополнительным представлением с другим представлением сбора внутри ячеек представления сбора.

Я добавил заголовок представлений заголовков в качестве элемента специальных возможностей при их создании, чтобы попытаться придерживаться UIAccessibilityContainer: https://developer.apple.com/documentation/uikit/accessibility/uiaccessibilitycontainer. Что должно позволить мне соответствовать протоколу .landmark через общедоступный enum UIAccessibilityContainerType.

Оба не смогли позволить ориентирам перемещаться от заголовка одного дополнительного вида или заголовка к следующему дополнительному виду или заголовку. Первоначально я думал, что, возможно, это была ошибка протокола для Ориентиров в Доступности, но я заметил, что другие приложения также правильно используют навигацию по Ориентиру.

Code CollectionView с дополнительным видом:

struct Content {
let name: String
let color: UIColor

 init(name: String, color: UIColor) {
    self.name = name
    self.color = color
 }
}
class CollecitonViewWithCollectionView: UIViewController {
 let testContent = [Content(name: "AA", color: .red), Content(name: "BB", color: .green), Content(name: "CC", color: .yellow)]

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.accessibilityContainerType = .landmark
}
}

extension CollecitonViewWithCollectionView: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 10
}

func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool {
    return false
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as? CollectionViewCell else {
        return UICollectionViewCell()
    }
    cell.content = testContent
    return cell
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    guard let headerView = collectionView.dequeueReusableSupplementaryView(
        ofKind: UICollectionElementKindSectionHeader,
        withReuseIdentifier: "HeaderView",
        for: indexPath
        ) as? HeaderView

        else {
            return UICollectionReusableView()
    }

    return headerView
}


}

extension CollecitonViewWithCollectionView: UICollectionViewDelegate {

}

class InnerCollectionTestCell: UICollectionViewCell {
@IBOutlet weak var testLabel: UILabel!

}

class CollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

var content: [Content]?

override func awakeFromNib() {
    super.awakeFromNib()
    collectionView.dataSource = self
    collectionView.delegate = self
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return content?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InnerCollectionTestCell", for: indexPath) as? InnerCollectionTestCell else {
        return UICollectionViewCell()
    }
    cell.testLabel.text = content?[indexPath.row].name ?? "Failed"
    cell.backgroundColor = content?[indexPath.row].color ?? .black
    return cell
}

}


class HeaderView: UICollectionReusableView {
@IBOutlet weak var titleLabel: UILabel!
}

Code TableView с заголовком или пользовательским представлением заголовка:

 class TestCollectionCell: UICollectionViewCell {
    @IBOutlet weak var contentStringLabel: UILabel!
}


class TestCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!

var content: [Content]?

override func awakeFromNib() {
    super.awakeFromNib()
    collectionView.dataSource = self
    collectionView.delegate = self
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return content?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TestCollectionCell", for: indexPath) as? TestCollectionCell else {
        return UICollectionViewCell()
    }
    cell.backgroundColor = content?[indexPath.row].color ?? .black
    cell.contentStringLabel.text = content?[indexPath.row].name ?? "Zzz"
    return cell
}
}



class TableViewWithCollectionView: UIViewController {
 @IBOutlet weak var tableView: UITableView!

 let testContent = [Content(name: "A", color: .red), Content(name: "B", color: .green), Content(name: "C", color: .yellow)]

override func viewDidLoad() {
    super.viewDidLoad()

    let headerNib = UINib(nibName: "TableViewHeaderFooterView", bundle: nil)
     tableView.register(headerNib, forHeaderFooterViewReuseIdentifier:  "TableViewHeaderFooterView")

}

}

extension TableViewWithCollectionView: UITableViewDelegate {

}

extension TableViewWithCollectionView: UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
    return 10
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath) as? TestCell else {
        return UITableViewCell()
    }
    cell.content = testContent
    return cell
}

func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
    return false
}

   /*func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
tableView.headerView(forSection: section)?.accessibilityTraits |= UInt64(UIAccessibilityContainerType.landmark.rawValue)

    return "Test Without custom header"
    }*/

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "TableViewHeaderFooterView") as? TableHeaderFooterView
    headerView?.tableHeaderTitleLabel.text = "TEST with custom header"
    headerView?.accessibilityTraits |= UInt64(UIAccessibilityContainerType.landmark.rawValue)
    return headerView
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50
}

}

Я знаю, что для заголовков вам нужно либо установить черту в IB, либо сделать:

//set here because Xcode is not giving me the option in IB
accessibilityTraits |= UIAccessibilityTraitHeader

Я бы предположил, что есть какой-то способ для Ориентиров?

1 Ответ

0 голосов
/ 13 августа 2019
override var accessibilityLabel: String? {
    get { return titleLabel.accessibilityLabel }
    set {}
}

override var accessibilityTraits: UIAccessibilityTraits {
    get { return UIAccessibilityTraits.header }
    set {}
}

override var accessibilityContainerType: UIAccessibilityContainerType {
    get { return UIAccessibilityContainerType.landmark }
    set {}
}

Таким образом, проблема заключается в том, что эти значения устанавливаются внутри повторно используемого заголовка, по-видимому, это вызывает проблемы с правильностью установки элементов доступности, и вместо этого их необходимо устанавливать в самих представлениях. Добавьте приведенный выше код в свои файлы HeaderView.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...