preferredFocusedViewis not overridden to return the
UIButton, the focus moves around the
UICollectionViewas expected, but the
UICollectionViewCelldoes not receive focus (does not get the white background with shadow), as evidenced by firings of
focusedproperty of the
UIButtonwhen moving through the
nextFocusedIndexPath) in that delegate method remains
The reason for overriding canBecomeFocused is because the docs say that the default value is false. Regardless, removing the overridden method doesn't change the behavior I'm seeing.
Here's a minimum working example. Reproduce yourself by (project posted separately to avoid moderation):
- New Single View project (Obj-C this time).
- Change ViewController to a UICollectionViewController.
- Swap out Main.storyboard's ViewController for a UICollectionViewController, changing class to ViewController.
- Add a new UICollectionViewController subclass.
- Populate a single UIButton in the prototype cell in the storyboard, changing the class (to that from step 4) and reuse ID, and adding an outlet to the subclass created in step 4.
- Populate required UICollectionViewDataSource methods in ViewController (numberOfItems..., and cellForItem...).
- Populate collectionView:didUpdateFocus... to observe focus changes.
Without overriding preferredFocusView in TestCollectionViewCell, you can move around the UICollectionView fine, but the UIButton (the only view in the cell) never gets focus. If you return the button from preferredFocusedView, the button gets focus, but only in (0,0).
This works! This also makes no sense on immediate inspection: the cell cannot obtain focus, but one of its child views can? This lead me to discover something else. UICollectionView has a property, remembersLastFocusedIndexPath, that I set to true. If this is false, I do not need to implement canFocusItemAtIndexPath. Reading the documentation a little more thoroughly, I think I see what may have been happening. A reading of the documentation for this property along with mwhuss's initial reply may reveal that this isn't a bug, but perhaps really strange default behavior (in my opinion).