Instance Method

collectionView(_:shouldUpdateFocusIn:)

Asks the delegate whether a change in focus should occur.

Declaration

optional func collectionView(_ collectionView: UICollectionView, shouldUpdateFocusIn context: UICollectionViewFocusUpdateContext) -> Bool

Parameters

collectionView

The collection view object requesting this information.

context

The context object containing metadata associated with the focus change. This object contains the index path of the previously focused item and the item targeted to receive focus next. Use this information to determine if the focus change should occur.

Return Value

true if the focus change should occur or false if it should not.

Discussion

Before a focus change can occur, the focus engine asks all affected views if such a change should occur. In response, the collection view calls this method to give you the opportunity to allow or prevent the change. Return this method to prevent changes that should not occur. For example, you might use it to ensure that the navigation between cells occurs in a specific order.

If you do not implement this method, the collection view assumes a return value of true.

If you subclass UICollectionView, you can also implement the same behavior by overriding the shouldUpdateFocus(in:) method, which is defined by the UIFocusEnvironment protocol and adopted by all views.

See Also

Managing Focus in a Collection View

func collectionView(UICollectionView, canFocusItemAt: IndexPath) -> Bool

Asks the delegate whether the item at the specified index path can be focused.

func indexPathForPreferredFocusedView(in: UICollectionView) -> IndexPath?

Asks the delegate for the index path of the cell that should be focused.