UICollectionViewCell 到 UIButton 焦点
UICollectionViewCell to UIButton Focus
我的问题与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'']>
看起来您在视图层次结构中没有 CollectionView
,这意味着您没有将 collectionView
添加为 view
的 subView
。
请确保在应用任何约束之前将 collectionView
添加为 subview
。你可以像下面那样做
self.view.addSubView(myCollectionView)
在前面添加这样的代码
self.view.addConstraint(topButtonFocusGuide.topAnchor.constraintEqualToAnchor(myCollectionView.bottomAnchor))
更新:
新错误是因为您没有使用 UICollectionView 注册单元格,在创建集合视图对象后添加此行。
[myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
我认为您需要阅读有关集合视图的更多信息,here 是一个很好的入门教程 UICollectionView
Apple docs 关于这个话题
我的问题与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'']>
看起来您在视图层次结构中没有 CollectionView
,这意味着您没有将 collectionView
添加为 view
的 subView
。
请确保在应用任何约束之前将 collectionView
添加为 subview
。你可以像下面那样做
self.view.addSubView(myCollectionView)
在前面添加这样的代码
self.view.addConstraint(topButtonFocusGuide.topAnchor.constraintEqualToAnchor(myCollectionView.bottomAnchor))
更新: 新错误是因为您没有使用 UICollectionView 注册单元格,在创建集合视图对象后添加此行。
[myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
我认为您需要阅读有关集合视图的更多信息,here 是一个很好的入门教程 UICollectionView
Apple docs 关于这个话题