简体   繁体   中英

How to change focus programmatically in tvOS

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM