Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force focus to change to a specific view in tvOS?

I am implementing custom code to handle a click on the Menu button on the Siri Remote. How can I force focus to change to my custom menu when pressing the menu button?

like image 600
Simen Øian Gjermundsen Avatar asked Sep 24 '15 13:09

Simen Øian Gjermundsen


5 Answers

For ios 10 you should use preferredFocusEnvironments instead of preferredFocusedView .

In below example if you want to focus on button then see below code.

@IBOutlet weak var button: UIButton!

override var preferredFocusEnvironments: [UIFocusEnvironment] {
    return [button]
}

override func viewDidLoad() {
    super.viewDidLoad()

    setNeedsFocusUpdate()
    updateFocusIfNeeded()
}
like image 188
vikas prajapati Avatar answered Oct 23 '22 14:10

vikas prajapati


Finally figured it out myself. You have to override the preferredFocusedView property of your UIView or UIViewController.

In Swift it works like this:

func myClickHandler() {
    someCondition = true
    self.setNeedsFocusUpdate()
    self.updateFocusIfNeeded()
    someCondition = false
}

override weak var preferredFocusedView: UIView? {
    if someCondition {
        return theViewYouWant
    } else {
        return defaultView
    }
}

I can't quite remember how to override getters in Objective-C so if someone want to post that I'll edit the answer.

like image 19
Slayter Avatar answered Oct 23 '22 13:10

Slayter


Here is another implementation based on Slayters answer above. Its slightly more elegant I think than using the conditional booleans.

Put this in your viewcontroller

var viewToFocus: UIView? = nil {
    didSet {
        if viewToFocus != nil {
            self.setNeedsFocusUpdate();
            self.updateFocusIfNeeded();
        }
    }
}

override weak var preferredFocusedView: UIView? {
    if viewToFocus != nil {
        return viewToFocus;
    } else {
        return super.preferredFocusedView;
    }
}

Then to use it in your code

viewToFocus = myUIView;
like image 16
Brandon Gill Avatar answered Oct 23 '22 14:10

Brandon Gill


here is the objective C

- (UIView *)preferredFocusedView
{
    if (someCondition) {
      // this is if your menu is a tableview
        NSIndexPath *ip = [NSIndexPath indexPathForRow:2 inSection:0];
        UITableViewCell * cell = [self.categoryTableView cellForRowAtIndexPath:ip];

        return cell;
    }

    return self.view.preferredFocusedView;

}

in your viewDidLoad or view did appear do something like this:

    UIFocusGuide *focusGuide = [[UIFocusGuide alloc]init];
    focusGuide.preferredFocusedView = [self preferredFocusedView];
    [self.view addLayoutGuide:focusGuide];

if you want to do it when it first launches

like image 6
Jenel Ejercito Myers Avatar answered Oct 23 '22 12:10

Jenel Ejercito Myers


Here's a nice little Swift 2 copy / paste snippet:

var myPreferredFocusedView: UIView?
override var preferredFocusedView: UIView? {
    return myPreferredFocusedView
}
func updateFocus(to view: UIView) {
    myPreferredFocusedView = napDoneView
    setNeedsFocusUpdate()
    updateFocusIfNeeded()
}

Use it like this:

updateFocus(to: someAwesomeView)
like image 1
Wiingaard Avatar answered Oct 23 '22 13:10

Wiingaard