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

放肆的年华 提交于 2020-01-01 08:33:22

问题


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?


回答1:


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.




回答2:


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()
}



回答3:


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;



回答4:


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




回答5:


@elu5ion 's answer, but in objective-c first declare:

@property (nonatomic) UIView *preferredView;

Set these methods:

-(void)setPreferredView:(UIView *)preferredView{
      if (preferredView != nil) {
         _preferredView = nil;
         UIFocusGuide *focusGuide = [[UIFocusGuide alloc]init];
         [self.view addLayoutGuide:focusGuide];
         focusGuide.preferredFocusedView = [self preferredFocusedView];
         [self setNeedsFocusUpdate];
         [self updateFocusIfNeeded];
    }
    _preferredView = preferredView;
 }


 - (UIView *)preferredFocusedView {
     if (_preferredView) {
          return _preferredView;
     }
     return self.view.preferredFocusedView;
}



回答6:


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)


来源:https://stackoverflow.com/questions/32762840/how-can-i-force-focus-to-change-to-a-specific-view-in-tvos

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