tvOS 10. PreferredFocusView is deprecated. How do I change to preferredFocusEnvironment?

社会主义新天地 提交于 2019-12-22 10:38:46

问题


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.

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

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

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

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

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];
    }
}

Thanks


回答1:


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];
}

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



来源:https://stackoverflow.com/questions/39479180/tvos-10-preferredfocusview-is-deprecated-how-do-i-change-to-preferredfocusenvi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!