Reputation: 32071
I have a collection view with a standard horizontal layout. Upon presenting a view controller and then dismissing it, the collection view reset focus back to the first cell, even though the last focused cell was not that one.
I've set
self.collectionView.remembersLastFocusedIndexPath = YES;
What's weird is that this only happens when I push a view controller on my navigation controller.
So if I do
[self.navigationController pushViewController:controller animated:YES];
and then dismiss, remembersLastFocusedIndexPath
does not work properly.
However, if I:
[self presentViewController:controller animated:YES completion:nil];
Then it works as expected.
Any idea why it wouldn't work via a navigation controller?
Upvotes: 10
Views: 4682
Reputation: 12363
navigationController?.pushViewController(details, animated: false)
present(details, animated: false)
It seems in the far past some workarounds would make it work. There are no workarounds presently. It is just "utterly broken".
Unfortunately, all you can do is file it under the usual heading "You must be joking, Apple."
Upvotes: 0
Reputation: 4521
You can also try to implement similar behaviour. Just remember last focused cell in
func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
and use this information in
func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool
Upvotes: 0
Reputation: 2147
make sure that you don't reloadData() on viewWillAppear of your collectionView or TableView. Otherwise rememberedFocus will be reseted to default (for collection view in my case it was the centre visible cell)
Upvotes: 5
Reputation: 161
What worked for me was ensuring that preferredFocusView on the viewController was the collection view:
override weak var preferredFocusedView: UIView? {
return collectionView
}
After this remembersLastFocusedIndexPath seems to work.
Upvotes: 7
Reputation: 23996
By default, the system remembers the position. You should probably fix this issue by setting remembersLastFocusedIndexPath
to false.
You could also implement the UICollectionViewDelegate
method:
func indexPathForPreferredFocusedViewInCollectionView(collectionView: UICollectionView) -> NSIndexPath?
The documentation tells us:
The functionality of this delegate method is equivalent to overriding the UICollectionView class’s preferredFocusedView method in the UIFocusEnvironment protocol. If the collection view’s remembersLastFocusedIndexPath method is set to YES, this method defines the index path that gets focused when the collection view is focused for the first time.
But for your issue, setting remembersLastFocusedIndexPath
to false should fix it.
Upvotes: 1