How to get buttons in a cell in a UICollectionView running on tvOS (AppleTV) to focus?

Here is my very simple code. It displays five cells in a UICollectionView (each cell with a simple button in it). All buttons point to a single IBAction method. All of this is customizable in Main.storyboard.

I can't get tvOS to focus on buttons. Any idea why?

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    return cell;
}

- (IBAction)buttonAction
{
    // do stuff
}

@end

enter image description here

+4
source share
2 answers

UICollectionViewCell , , , preferredFocusedView , .

- (UIView *)preferredFocusedView
{
    return _button;
}

+12

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

Source: https://habr.com/ru/post/1607204/


All Articles