gpt4 book ai didi

swift - UICollectionViewCell 到 UIButton 焦点

转载 作者:行者123 更新时间:2023-11-30 13:18:56 26 4
gpt4 key购买 nike

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

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