如何在tvOS中以编程方式更改焦点 [英] How to change focus programmatically in tvOS
问题描述
UITabBarController
。 我的要求是在10秒后自动隐藏tabbar,焦点可以移动到tabbar项目内的
AVPlayerViewController
。 我试图覆盖
preferredFocusedView
,但是焦点不能移动到 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 $ b $ setNeedsFocusUpdate()
updateFocusIfNeeded( )
}
覆盖var preferredFocusedView:UIView? {
return self.playerController.view
}
请教我如何以编程方式移动焦点。 $ c>当时不在 viewController
中,而焦点在 tabBarController
中,所以我建议你做像这样的
创建一个 TabBarController
子类,并在故事板中设置你的tabController类到该类,然后在 tabBarController
subClass做这样的事情。
#importTabBarViewController.h
@interface TabBarViewController()
@end
@implementation TabBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tabBar.alpha = 0;
//当你需要的时候再设置alpha = 1。
$ b - (UIView *)preferredFocusedView
{
//你也可以在这里添加一些if
return [self.selectedViewController preferredFocusedView]; //或者你可以做self.selectedViewController.view如果这个视图是可以关注的
$ b $ end
code $ pre $
我在这里选择的视图控制器是FirstViewController,它看起来像这样并且工作正常。
pre $ #importFirstViewController.h
@interface FirstViewController()
@property(弱,非原子)IBOutlet UIButton * button;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
$ b - (UIView *)preferredFocusedView
{
return _button; //返回您想要关注的视图
}
@end
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
这篇关于如何在tvOS中以编程方式更改焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!