Search code examples
objective-cuicollectionviewtvos

Focus for UICollectionViewCells on tvOS


I'm new to tvOS. I have created UICollectionView using a XIB file in Objective-C. How can I focus UICollectionViewCell?

I have a label and an image view in the cell, and I want to focus both the label and the image view.


Solution

  • UICollectionViewCell are not focus appearance by default, but you can achive this by adding one yourself.

    - (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
    {
        if (self.focused)
        {
            // Apply focused appearence,
            // e.g scale both of them using transform or apply background color 
        }
        else
        {
           // Apply normal appearance
        }
    }
    

    OR

    If you just want to focus ImageView like scaling it up when collection view cell get focus you can do like this in awakeFromNib method

    self.imageView.adjustsImageWhenAncestorFocused = YES;
    self.clipsToBounds = NO;