tvOS 10. 不推荐使用 PreferredFocusView.如何更改为 preferredFocusEnvironment? [英] tvOS 10. PreferredFocusView is deprecated. How do I change to preferredFocusEnvironment?

查看:84
本文介绍了tvOS 10. 不推荐使用 PreferredFocusView.如何更改为 preferredFocusEnvironment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个子视图,我已经为其覆盖了 preferredFocusedView.该子类有一个名为 viewToFocus 的 UIView.我检查该视图是否存在,如果存在,我将聚焦该视图,如果不存在,则返回父级的 preferredFocusedView.

I have a subview for which I have overridden the preferredFocusedView. The subclass has a UIView called viewToFocus. I check if that view exists, if it does I focus that view, if not I return the preferredFocusedView of the parent.

自从我今天更新到 tvOS 10 以来,我收到以下错误:

Since I updated to tvOS 10 today I am getting the following error:

'preferredFocusedView' 已弃用:首先在 tvOS 10.0 中弃用 -改用 -preferredFocusEnvironments.

'preferredFocusedView' is deprecated: first deprecated in tvOS 10.0 - Use -preferredFocusEnvironments instead.

我在文档中找不到任何说明如何实现 preferredFocusEnvironment 的地方.在文档中找到 在你的应用中支持焦点,它说

I cant find anywhere in the documentation that explains how preferredFocusEnvironment is to be implemented. In the documentation found Supporting Focus within Your App, it says to

覆盖 preferredFocusedView 以指定焦点应该从哪里开始默认情况下.

Override the preferredFocusedView to specify where focus should start by default.

我尝试添加 UIFocusEnvironment 协议,但我不确定如何用它替换preferredFocusedView"的功能.

I tried adding the UIFocusEnvironment Protocol but I am not sure how to replace the functionality of 'preferredFocusedView' with it.

- (UIView *)preferredFocusedView {
    if (self.viewToFocus) {
        UIView *newView = self.viewToFocus;
        self.viewToFocus = nil;

        return newView;
    } else {
        return [super preferredFocusedView];
    }
}

谢谢

推荐答案

作为 preferredFocusEnvironments 调用的结果,您需要传递一组视图,而不是像以前那样只传递一个视图.此视图必须按焦点优先级排序.因此,如果您的 UIViewController 上有 3 个 UIButton 项目,preferredFocusEnvironments 属性可以实现如下:

You need to pass an array of views as a result of preferredFocusEnvironments call instead of just one view as it was before. This views must be ordered by the focus priority. So, if you have 3 UIButton items on your UIViewController the preferredFocusEnvironments property can be implemented the following:

- (NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments {

    return @[self.b3, self.b2, self.b1];
}

在你的情况下,你只需要返回一个 @[newView] 作为结果.

In your case you just need to return a @[newView] as a result.

这篇关于tvOS 10. 不推荐使用 PreferredFocusView.如何更改为 preferredFocusEnvironment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆