gpt4 book ai didi

swift - Collection View 中带有补充 View 的 tvOS 地标

转载 作者:IT王子 更新时间:2023-10-29 05:44:54 26 4
gpt4 key购买 nike

我正在尝试从 Apple 的电影应用程序复制 Landmark Accessibility 的流程。我尝试使用带有自定义标题的 TableView 和标准标题 View ,其中我的单元格内部有一个 Collection View ,以及一个带有补充 View 的 Collection View , Collection View 单元格中有另一个 Collection View 。

我在创建标题 View 标题时将其添加为可访问性元素以尝试遵守 UIAccessibilityContainer:https://developer.apple.com/documentation/uikit/accessibility/uiaccessibilitycontainer .这应该允许我通过公共(public)枚举 UIAccessibilityContainerType 遵守 .landmark 协议(protocol)。

两者都未能允许地标从一个补充 View 或标题的标题移动到下一个补充 View 或标题。我最初认为这可能是辅助功能中 Landmarks 协议(protocol)的错误,但我注意到其他应用程序也正确使用了 Landmark 导航。

带有补充 View 的代码 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!
}

使用 headerView 或自定义标题 View 对 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

我想 Landmarks 有这样的方法吗?

最佳答案

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 {}
}

所以问题是这些值是在可重用 header 内设置的,显然这会导致正确设置可访问性元素的问题,而需要在 View 本身上设置它们。将以上代码添加到您的 HeaderView 文件中。

关于swift - Collection View 中带有补充 View 的 tvOS 地标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52373562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com