8

我有一个标准水平布局的集合视图。在呈现视图控制器然后将其关闭时,集合视图会将焦点重置回第一个单元格,即使最后一个焦点单元格不是那个。

我已经设置

self.collectionView.remembersLastFocusedIndexPath = YES;

奇怪的是,这只发生在我在导航控制器上推送视图控制器时。

所以如果我这样做

[self.navigationController pushViewController:controller animated:YES];

然后解雇,remembersLastFocusedIndexPath不能正常工作。

但是,如果我:

[self presentViewController:controller animated:YES completion:nil];

然后它按预期工作。

知道为什么它不能通过导航控制器工作吗?

4

4 回答 4

7

对我有用的是确保 viewController 上的 preferredFocusView 是集合视图:

override weak var preferredFocusedView: UIView? {
    return collectionView
}

在这之后 remembersLastFocusedIndexPath 似乎工作。

于 2016-01-08T03:44:52.030 回答
5

确保不要在 collectionView 或 TableView 的 viewWillAppear 上重新加载数据()。否则,rememberedFocus 将被重置为默认值(在我的情况下,对于集合视图,它是中心可见单元格)

于 2016-02-24T09:49:31.777 回答
1

默认情况下,系统会记住该位置。您可能应该通过设置remembersLastFocusedIndexPath为 false 来解决此问题。

您还可以实现该UICollectionViewDelegate方法: func indexPathForPreferredFocusedViewInCollectionView(collectionView: UICollectionView) -> NSIndexPath?

文档告诉我们:

此委托方法的功能相当于在 UIFocusEnvironment 协议中重写 UICollectionView 类的 preferredFocusedView 方法。如果集合视图的 remembersLastFocusedIndexPath 方法设置为 YES,则此方法定义当集合视图第一次获得焦点时获得焦点的索引路径。

但是对于您的问题,设置remembersLastFocusedIndexPath为 false 应该可以解决它。

于 2015-10-01T12:06:13.320 回答
0

您也可以尝试实现类似的行为。只记得最后一个集中的单元格

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

并将这些信息用于

func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool
于 2016-08-11T07:32:36.290 回答