【问题标题】:tvOS: Setting focus ordertvOS:设置焦点顺序
【发布时间】:2015-10-21 19:30:53
【问题描述】:

我目前有 3 个名为 MenuButton 的对象,它们是 UIButton 的子对象。如图所示,这些是在一行中定义的。

我在 MenuButton -didUpdateFocusInContext:withAnimationCoordinator 方法中定义了高亮状态:

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context
       withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
    if (context.nextFocusedView == self)
    {
        [self setTitleColor:[UIColor whiteColor]
                   forState:UIControlStateNormal];

    }
    else if (context.previouslyFocusedView == self)
    {
        [self setTitleColor:[UIColor colorWithRed:25.0/255.0
                                              green:23.0/255.0
                                               blue:16.0/255.0
                                              alpha:1.0]
                     forState:UIControlStateNormal];
    }

}

我还通过以下方式在 MenuButton 对象中设置了 canBecomeFocused:

- (BOOL)canBecomeFocused
{
    return YES;
}

我已经使用以下方法设置了我的初始焦点按钮:

[_camerasButton setNeedsFocusUpdate]

当我在模拟器遥控器上向右滑动时,我无法让它专注于交付。

谁能解释我如何使用 UIFocusGuide 在 Objective-C 中正确实现这一点?

更新 1 根据 JustinVoss 对第一条评论的建议,我设置了断点并在调试控制台上执行了 _whyIsThisViewNotFocusable。收到此提示:

(lldb) po [(UIView*)0x7ff68054f7c0 _whyIsThisViewNotFocusable] 
ISSUE: This view may not be focusable because other views or focus guides are occluding it.

我正在定义我的 MenuButton 框架,如图所示:

MenuButton *deliveriesButton = [MenuButton buttonWithType:UIButtonTypeCustom];
    [deliveriesButton setTitle:@"Deliveries" forState:UIControlStateNormal];
    [deliveriesButton setTitleColor:[UIColor colorWithRed:25.0/255.0
                                                 green:23.0/255.0
                                                  blue:16.0/255.0
                                                 alpha:1.0]
                        forState:UIControlStateNormal];
    [deliveriesButton.titleLabel setFont:[Constants lightFontOfSize:39]];
    [deliveriesButton sizeToFit];

    deliveriesButton.center = CGPointMake(viewWidth/2,
                                       menuBarHeight / 2);

    [self.view addSubview:deliveriesButton];
    _deliveriesButton = deliveriesButton;

【问题讨论】:

  • 你尝试过各种焦点调试技术吗?如果焦点没有移动的原因是因为两个视图的框架没有很好地对齐,那么焦点指南可以提供帮助,但这似乎不是你的问题。您可能想尝试 _whyIsThisViewNotFocusable 方法来找出“Deliveries”无法聚焦的原因。 (详见我的回答:stackoverflow.com/a/32831091/5616
  • 嗨贾斯汀,感谢您的回答。我设置了一个断点并在 logoutButton 上执行了 _whyIsThisViewNotFocusable 并收到此提示:“问题:此视图可能无法聚焦,因为其他视图或焦点指南正在遮挡它。”。有什么建议吗?
  • 按钮顶部必须有另一个视图(可能是透明的),这导致焦点引擎无法“看到”您的按钮。检查您的视图层次结构,看看该区域是否有任何东西可能会阻塞您的按钮。

标签: objective-c xcode uibutton tvos


【解决方案1】:

使用上面 cmets 中 Justin Voss 引用的解决方案,我浏览了我的 UIViewController 中的 UIViews 并设置:

view.userInteractionEnabled = NO;

在我不需要选择的任何视图上(包括背景 UIImageView 对象)。这解决了问题。

希望这对其他人有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 2020-02-15
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多