1

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.

Hamish
  • 78,605
  • 19
  • 187
  • 280
Vipulk617
  • 799
  • 1
  • 7
  • 23

1 Answers1

2

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
Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
  • TabBar hidden in particular time but focus still remain on TabBar. – Vipulk617 Feb 04 '16 at 12:15
  • When I call self.preferredFocusedView in TabBarController class in didSelectViewController method then it's not change focus in AVPlayerViewController. – Vipulk617 Feb 04 '16 at 12:36
  • 3
    you should not call this method itself directly, it will call at start it self, or you should call setNeedUpdateFocus method to re query this method – Adnan Aftab Feb 04 '16 at 12:38
  • 2
    preferredFocusedView is deprecated for ios 10. can you please tell me how to use preferredFocusEnvironments instead of that. – vikas prajapati Nov 18 '16 at 04:27