The “preferred focus” principle works conceptually in that there is a “chain” of objects, starting from the window of your application, going down through the view and view controllers until the chain finishes the focused view. This look at the end is what will be focused when your application starts, or if the focus needs to be reset (for example, if the current focus image is removed from the view hierarchy, UIKit will need to select another view to become focused). The preferredFocusedView method is how you define the chain.
For example, UIWindow implements preferredFocusedView to return the preferred focused view of the rootViewController . In the abstract, t doing something like this:
- (UIView *)preferredFocusedView { return [self.rootViewController preferredFocusedView]; }
UIViewController implements preferredFocusedView to return its view property. UIView just returns itself.
However, UICollectionView implements preferredFocusedView in different ways: in the simplest case, it returns the first cell. So part of what you want is already done for you. If the focus does not move to the first cell of your collection, then the problem in the chain is the problem.
If the view of the collection of the view manager is not a view property of the view controller, you need to direct the focus chain to the collection directly:
// in your view controller: - (UIView *)preferredFocusedView { return myCollectionView; }
From there, the collection view will direct the chain to a specific cell.
source share