Can't focus UICollectionViewCell inside UITableViewCell
Asked Answered
P

2

8

so I am exploring the new tvOS. As I wanted to kind of replicate the horizontal feature section of the app store (e.g. "Best New Apps")

My plan was to use a UITableView to lay out the base structure (rows) and use an UICollectionView for all the individual horizontally scrollable items.

Unfortunately the tvOS always focuses the UITableViewCell, not the UICollectionViewCell inside. I've already read about the

override var preferredFocusedView: UIView? {
    return aView
}

So using this inside my controller is kind of hard regarding getting the correct subview.

Is there anything I could look into or could someone please point me in the right direction? Iterating through all subviews until I get the correct one seems like a bad idea.

Thanks a lot!

Payment answered 8/10, 2015 at 15:26 Comment(0)
A
21

I tried out a similar situation where I had a CollectionView within a CollectionView to get the setup you described. Let's call them innerCV and outerCV. To get innerCV to be focused and not outerCV's cells, I set this delegate method to return false for outerCV.

- (BOOL)collectionView:(UICollectionView *)collectionView canFocusItemAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

In turn, innerCV's cells are now focusable as opposed to outerCV's cells.

Credit for this goes to Apple. I used their sample project as a guide to start. Check out the Focus samples within their project.

Sample Project: Apple tvOS UIKitCatalog

Antheridium answered 9/10, 2015 at 20:44 Comment(5)
Hey Richard, thanks a lot for your help. I've already tried that and unfortunately that didn't work for my setup, which really made me think that I might have messed something up there. I think I just gonna set a base project up again an test it.Payment
Thanks Richard, this issue is solved by Apple on the sample project.Uis
@Uis What is the fix then?Used
@JeremyHicks it's not a fix, what I mean is that Apple shows their implementation for this on their sample project. Check out the link.Uis
Life saver! Thx!Hormone
C
1

I used a different approach..

Setting the tableview selection property to No Selection in storyboard makes the focus engine to focus on UICollectionViewCell cell rather than UITableViewCell.

Hope it helps :)

Image

Chincapin answered 6/12, 2016 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.