Help with focus on subview/collectionviewcell

Hi,


I was wondering if anyone could give some tips on how to achieve this:


I have a tableview with a number of cells.

Each UITableCiewCell subclass has a UICollectionView with horizontal flowlayout (and scroll) -- pretty much like some big VOD provider, that shall remain unnamed here, does in their current (old) Apple TV app.

How can i get the tableviewcells to not get focus and while passing on, allowing focus to go to individual collectionview cells? Is this possible?


(

tableView:canFocusRowAtIndexPath:
does not seem to do it, would have been really nice if this would just allow the collectionview's cells to get focus.. )
Answered by in 52652022

You could also return NO in -canBecomeFocused in a UITableViewCell subclass.


What's happening in this case, is the table view cell still becoming focused? Or is focus not appear to be changing at all?


By default UICollectionViewCells do not have a focusable appearance. To see what view is focused after you attempt to move pause the debugger and check the currently focused view.


po [[UIScreen mainScreen] focusedView]


To do some more advanced focus debugging you can put a breakpoint in the following method in a view controller or view and then Quicklook the context object.


- (void)didUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context withAnimationCoordinator:(nonnull UIFocusAnimationCoordinator *)coordinator
Accepted Answer

You could also return NO in -canBecomeFocused in a UITableViewCell subclass.


What's happening in this case, is the table view cell still becoming focused? Or is focus not appear to be changing at all?


By default UICollectionViewCells do not have a focusable appearance. To see what view is focused after you attempt to move pause the debugger and check the currently focused view.


po [[UIScreen mainScreen] focusedView]


To do some more advanced focus debugging you can put a breakpoint in the following method in a view controller or view and then Quicklook the context object.


- (void)didUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context withAnimationCoordinator:(nonnull UIFocusAnimationCoordinator *)coordinator

Great answer, adding canBecomeFocused with false to my tableviewcell class fixed it. However, as you mention, there is no focusable appearence on the collectionview cells. But I was able to easily see which was becoming focused using something like this in the tableviewcells (that contains the horizontal flow layout collectionview):


override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        context.nextFocusedView?.backgroundColor = UIColor.greenColor()
        context.previouslyFocusedView?.backgroundColor = UIColor.clearColor()
    }

    override func canBecomeFocused() -> Bool {
        return false
    }

The delegate method -tableView:canFocusRowAtIndexPath: should have worked. It'd be great if you could file a bug report at http://bugreport.apple.com with an example project so we can look into it.

Hi again,


I am sorry for being a bit too unspecific in my answer. Basically both approaches work (just tried them both individually again).

( tableView:canFocusRowAtIndexPath: and canBecomeFocused )


The primary reason why I was being confused was because it was not visible in anyway what was happening (I couldn't see that the collectionview cells get focused). The po (print object) command was really useful in figuring out what was happening.

Also take a look at overriding preferredFocusedView in your view controller to instruct the focus engine which subview you would like to receive focus at a given moment.

Help with focus on subview/collectionviewcell
 
 
Q