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

1

3

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

Own answered 13/9, 2016 at 21:22 Comment(0)
C
9

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.

Camarata answered 14/9, 2016 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.