Friday, June 9, 2017

Implementing custom focus behaviour for UICollectionView on AppleTV

Leave a Comment

I'm trying to implement pretty straightforvard UI on tvOS and after few failures it seems like it is impossible.

I have 2 collection views with horizontal scrolling.

  • 1st - cities
  • 2nd - options

Option set is same for all cities but some options can be disabled for some cities. At least one option is available for each city.

User can select city and option for it by focusing on specific cell. Current selection is visible on screen (see labels under collection views)

UI example

What I'm trying to do is to remember selected city/option, so when I change option between thos collection views - previously selected item becomes focused (selected). That's possible to do using remembersLastFocusedIndexPath flag fo UICollectionView but collection view ignore disabled cells.

For example in attached project Option 3 is disabled for Dublin. So if user selects London, then selects Option 3, then moves focus back to cities collection view and changes selection to Dublin, then Option 3 becomes disabled (isUserInteractionEnabled is false and collectionView(_ collectionView:, canFocusItemAt indexPath:) returns false). However if remembersLastFocusedIndexPath is true - collection view ignores all of those and tries to focus disabled item. At this point UI ob collection view "broke":

broken UICollectionView

There is indexPathForPreferredFocusedView(in:) method but it's trigged only once for preferred UICollectionView even though that's isn't obvious from Apple's documentation.

Is there a way to write "custom" logic for focusing cells? What I need to implement for options collection view is to select currently selected option if it is available or first available option if currently selected option is disabled every time it gets focus. Subclassing UICollectionView should't be the way to solve that as it shouldn't know about data model.

Here you can download test project

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment