Focused item in UICollectionView not animated

I have a very simple UICollectionView built with Objective-C with cells containing just UIImageView. When browsing through cells (in simulator) I can see from logs that focus changes correctly but focused item is not zoomed like in Apple example code. Also while circling around the remote touchpad selected cell is not animated. What am I missing? Sample code is implemented in Swift so I have created a new Objective-C project instead. My ViewController implements UIFocusEnvironment method shouldUpdateFocusInContext returning YES and setting preferredFocusedView to UICollectionView. Also canFocusItemAtIndexPath and shouldUpdateFocusInContext return YES. UICollectionViewCells are created programmatically, not in interface builder. Could this be an issue? Any ideas how to debug further?

Accepted Reply

Got it working. Problem was that my UIImageView did not have any image set. So selecting an item does not zoom UIImageView object itself, it just zooms UIImageView.Image. Stupid me. Anyway, thanks for super-respossive Apple staff participating this thread / forum. Keep up the good work!

Replies

By default UICollectionViewCells don't have a focus appearance. But for your image views set -adjustsImageWhenAncestorFocused to YES and they should change their appearance when focused automatically.

Also in UICollectionViewCells initWithFrame method I set 'self.imageView.adjustsImageWhenAncestorFocused = true;'

Make sure that the property adjustsImageWhenAncestorFocused is set to true for your UIImageView. Alternatively, check the "Adjusts image when focused" box in your storyboard.

Here's the code to init cell:


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        CGRect rect = CGRectMake(0, 0, frame.size.height, frame.size.height);
        self.imageView = [[UIImageView alloc] initWithFrame: rect];
        self.imageView.adjustsImageWhenAncestorFocused = YES;
        self.imageView.clipsToBounds = false;
        [self.contentView addSubview: self.imageView];
    }
    return self;
}

Try removing your -preferredFocusView method. The focus engine will automatically find the focusable views.


You can also put a breakpoint in -didUpdateFocusInContext:withAnimationCoordinator: and after you try to change focus see what context.nextFocusedView.

Got it working. Problem was that my UIImageView did not have any image set. So selecting an item does not zoom UIImageView object itself, it just zooms UIImageView.Image. Stupid me. Anyway, thanks for super-respossive Apple staff participating this thread / forum. Keep up the good work!