【问题标题】:How do I get buttons in a cell on a UICollectionView running on tvOS (AppleTV) to take focus?如何在 tvOS (AppleTV) 上运行的 UICollectionView 上的单元格中获取按钮以获取焦点?
【发布时间】:2015-09-14 02:04:50
【问题描述】:

这是我非常基本的代码。它在 UICollectionView 中显示了五个单元格(每个单元格中都有一个简单的按钮)。所有按钮都指向单个 IBAction 方法。这是 Main.storyboard 中的所有设置。

不过,我无法让 tvOS 关注按钮。知道为什么吗?

#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

【问题讨论】:

    标签: objective-c uicollectionview apple-tv tvos


    【解决方案1】:

    默认情况下 UICollectionViewCell 是可聚焦的,这意味着它将获得焦点而不是其子视图,但您可以通过覆盖 preferredFocusedView 来覆盖此功能并返回您想要获得焦点的任何视图。

    - (UIView *)preferredFocusedView
    {
        return _button;
    }
    

    阅读更多关于Initial Focus and the Preferred Focus Chain

    【讨论】:

    • 对于 UICollectionviewCell 的任何自定义,您只需覆盖:覆盖 func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
    【解决方案2】:

    或者你也可以

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多