我的问题与 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/