草庐IT

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

coder 2023-07-16 原文

我正在尝试从 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/

有关swift - Collection View 中带有补充 View 的 tvOS 地标的更多相关文章

  1. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  2. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  3. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  4. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  5. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  6. ruby-on-rails - 在 haml View 中重构条件 - 2

    除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p

  7. ruby - Sinatra 找不到 View 目录 - 2

    我正在尝试以一种更类似于普通RubyGem结构的方式构建我的Sinatra应用程序。我有以下文件树:.├──app.rb├──config.ru├──Gemfile├──Gemfile.lock├──helpers│  ├──dbconfig.rb│  ├──functions.rb│  └──init.rb├──hidden│  └──Rakefile├──lib│  ├──admin.rb│  ├──api.rb│  ├──indexer.rb│  ├──init.rb│  └──magnet.rb├──models│  ├──init.rb│  ├──invite.rb│  ├─

  8. ruby-on-rails - 如何让 Rails View 返回其关联的操作名称? - 2

    我有一个非常简单的Controller来管理我的Rails应用程序中的静态页面:classPagesController我怎样才能让View模板返回它自己的名字,这样我就可以做这样的事情:#pricing.html.erb#-->"Pricing"感谢您的帮助。 最佳答案 4.3RoutingParametersTheparamshashwillalwayscontainthe:controllerand:actionkeys,butyoushouldusethemethodscontroller_nameandaction_nam

  9. ruby-on-rails - 如何从按钮或链接单击的 View 调用 Rails 方法 - 2

    基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub

  10. ruby-on-rails - 如何编写跨模型、 Controller 和 View 的 Rails mixin - 2

    为了减少我的小Rails应用程序中的代码重复,我一直致力于将我的模型之间的通用代码放入它自己的单独模块中,到目前为止一切顺利。模型的东西相当简单,我只需要在开头包含模块,例如:classIso这工作正常,但是现在,我将有一些Controller和View代码,这些代码也将在这些模型之间通用,到目前为止,我有这个用于我的可发送内容:#Thisisamodulethatisusedforpages/formsthatarecanbe"sent"#eitherviafax,email,orprinted.moduleSendablemoduleModeldefself.included(kl

随机推荐