swift - UICollectionViewCell 到 UIButton 焦点

标签 swift tvos apple-tv

我的问题与 UICollectionViewCell to UIButton Focus in tvOS 中描述的问题类似 但我的按钮位于 UICollectionView 下方。我尝试添加焦点引导,但我一定做错了什么。

let topButtonFocusGuide = UIFocusGuide()
topButtonFocusGuide.preferredFocusedView = myButton
self.view.addLayoutGuide(topButtonFocusGuide)

self.view.addConstraint(topButtonFocusGuide.topAnchor.constraintEqualToAnchor(myCollectionView.bottomAnchor))
self.view.addConstraint(topButtonFocusGuide.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor))
self.view.addConstraint(topButtonFocusGuide.leadingAnchor.constraintEqualToAnchor(myCollectionView.leadingAnchor))
self.view.addConstraint(topButtonFocusGuide.widthAnchor.constraintEqualToAnchor(myCollectionView.widthAnchor))

我收到此错误:

libc++abi.dylib: terminating with uncaught exception of type NSException
CostomLayout[18448:888102] The view hierarchy is not prepared for the 
constraint: <NSLayoutConstraint:0x7fb9e3c5e060 V:[UICollectionView:0x7fb9e501ac00]-(0)-[UIFocusGuide:0x7fb9e3c5b8b0'']>

最佳答案

View 层次结构中似乎没有 CollectionView,这意味着您尚未将 collectionView 添加为 view 的 subView 。 在应用任何约束之前,请确保将 collectionView 添加为 subview。您可以像下面这样做

self.view.addSubView(myCollectionView)

之前添加类似的代码

self.view.addConstraint(topButtonFocusGuide.topAnchor.constraintEqualToAnchor(myCollectionView.bottomAnchor))

更新: 新错误是因为您没有向 UICollectionView 注册单元格,在创建 Collection View 对象后添加此行。

[myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];

我认为您需要阅读有关 Collection View 的更多信息,here是一个很棒的 UICollectionView

入门教程

Apple docs关于这个话题

关于swift - UICollectionViewCell 到 UIButton 焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37954496/

相关文章:

iphone - 将数据从 Apple Watch 共享到 iPhone

swift - 如何只移动一次标签(快速)?

ios - 无法连接到苹果电视! "Making apple tv ready for development"显示对话框

ios - iCloud:在 iOS 和 tvOS 之间同步键值存储

ios - 我可以将新的第 4 代 AppleTV 变成 iBeacon 吗?

ios - 从 iOS 上的 mov 文件中提取元数据

ios - 如何在 tvOS 应用程序中为 collectionview 的节标题设置动画?

objective-c - 链接到开发者的应用程序

swift - TVOS 需要按下 2 次菜单按钮才能从根 Controller 退出到仪表板

ios - Realm Swift 设计模式