4

我是 tvOS 应用程序的新手。我在UICollectionViewCell里面创建了一个自定义按钮UICollectionView。当我加载视图控制器时,我将焦点移到 UICollectionView 的第一个单元格,但是通过按键盘上的下一个键,焦点不会移动到下一个单元格。

我的 UICollectionView 中有六个不需要滚动的单元格,这意味着这些单元格在 UICollectionView 的宽度内正确拟合。有六个单元格,我无法将焦点移到下一个单元格。但是,如果我向 UICollectionView 添加一个单元格,焦点将正确移动到下一个单元格。我不确定发生了什么。谁能帮我解决这个问题?

谢谢

4

2 回答 2

9

我也有嵌套集合视图。对我来说,问题是内部集合视图没有覆盖-(BOOL)canBecomeFocus和返回NO

帮助我调试此问题的是打开视图层次结构,获取未获得焦点的视图地址(通过右键单击视图的打印描述)并调用po [<view address> _whyIsThisViewNotFocusable],这很可能会告诉您问题所在。就我而言,它是这样写的:

Printing description of $3:
<_UIStackedImageContainerView: 0x7f9404604630; frame = (0 0; 308 308); layer = <_UIStackedImageContainerLayer: 0x7f94046047d0>>
(lldb) po [0x7f9404604630 _whyIsThisViewNotFocusable]
ISSUE: This view returns NO from -canBecomeFocused.
ISSUE: One or more ancestors have issues that may be preventing this view from being focusable. Details: 

    <StationCollectionViewCell 0x7f940434c6c0>:
        ISSUE: This view returns YES from -canBecomeFocused, which will prevent its subviews from being focusable.

    <StationSectionCollectionViewCell 0x7f940403e8e0>:
        ISSUE: This view returns YES from -canBecomeFocused, which will prevent its subviews from being focusable.
于 2016-03-14T14:06:37.483 回答
2

在斯威夫特

如果你想关注collection view Cell 那么你可以使用collectionview Delegate 方法,方法名是

func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) 
{
}

你可以像这样使用这种方法......

func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

if let previousIndexPath = context.previouslyFocusedIndexPath,
let cell = collectionView.cellForItemAtIndexPath(previousIndexPath) {
cell.contentView.layer.borderWidth = 0.0
cell.contentView.layer.shadowRadius = 0.0
cell.contentView.layer.shadowOpacity = 0
}

if let indexPath = context.nextFocusedIndexPath,
let cell = collectionView.cellForItemAtIndexPath(indexPath) {
cell.contentView.layer.borderWidth = 8.0
cell.contentView.layer.borderColor = UIColor.blackColor().CGColor
cell.contentView.layer.shadowColor = UIColor.blackColor().CGColor
cell.contentView.layer.shadowRadius = 10.0
cell.contentView.layer.shadowOpacity = 0.9
cell.contentView.layer.shadowOffset = CGSize(width: 0, height: 0)
collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: [.CenteredHorizontally, .CenteredVertically], animated: true)
}
} 
于 2016-09-12T11:48:05.100 回答