How to change focus programmatically in tvOS

不打扰是莪最后的温柔 提交于 2020-01-05 09:08:54

问题


I am developing a tvOS app in swift. I am using UITabBarController in the app. My requirement is to hide the tabbar automatic after 10 seconds and focus can move to AVPlayerViewController inside the tabbar item. I tried to override preferredFocusedView, but focus cannot move to AVPlayerViewController.

func updateFocus() {

    self.playerController.view.hidden = false
    self.playerController.view.alpha = 1.0
    self.playerController.view.userInteractionEnabled = true
    self.playerController.view.layer.zPosition = 1.0
    self.preferredFocusedView
    setNeedsFocusUpdate()
    updateFocusIfNeeded()
}

override var preferredFocusedView: UIView? {

    return self.playerController.view

}

Please suggest me how to move focus programmatically.


回答1:


Issue is focus is not in viewController at that time instead focus is in tabBarController, so what I will suggest you is do something like this, Create a TabBarController subClass and set class of your tabController in story board to that class, and then in tabBarController subClass do something like this.

  #import "TabBarViewController.h"

    @interface TabBarViewController ()

    @end

    @implementation TabBarViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.tabBar.alpha = 0;
        // set alpha = 1 back again when you need.
    }

    - (UIView*)preferredFocusedView
    {
// you can also add some if else here
        return [self.selectedViewController preferredFocusedView];// or you can do self.selectedViewController.view if that view is focusable
    }

    @end

My selected view controller here is FirstViewController which look like this and its working fine.

#import "FirstViewController.h"

@interface FirstViewController ()
@property (weak, nonatomic) IBOutlet UIButton *button;

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (UIView*)preferredFocusedView
{
    return _button;// return view which you want to make focus able
}
@end


来源:https://stackoverflow.com/questions/35197806/how-to-change-focus-programmatically-in-tvos

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