Search code examples
objective-cuiviewapple-tvtvos

How do you set canBecomeFocused for a custom view


I am making an application for tvOS. I have a view that contains a UIButton and a custom UIView that contains a couple other custom views. The simulator is only able to highlight the UIButton and not the custom view.

According to the Building Apple TV Apps Docs:

If your custom view needs to be focusable, override canBecomeFocused to return YES (by default, it returns NO).

According to the canBecomeFocused Docs:

canBecomeFocused will return

YES if the view can become focused; NO otherwise.

However, attempting to assign YES to canBecomeFocused by doing this:

self.customView.canBecomeFocused = YES;

Gives this error:

No setter method 'setCanBecomeFocused:' for assignment to property

How do I accomplish this?


Solution

  • It looks like UIView declares the function/property.

    Have you tried overriding the function like so?

    Swift

    override func canBecomeFocused() -> Bool {
        return true
    }
    

    Objective-C

    - (BOOL)canBecomeFocused {
        return YES;
    }
    

    I haven't tried this, but it may work for you.