How to Make a Uiview Focusable Using the Focus Engine on Apple Tv

How to make a UIView focusable using the focus engine on Apple TV

So the problem was that I didn't notice that my cell got focus. To wrap this up, you need to implement

1) override canBecomeFocused

2) override "didUpdateFocusInContext:withAnimationCoordinator:" method to be able to highlight the cell as focused

Swift 2.3:

override func canBecomeFocused() -> Bool {
return true
}

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

if context.nextFocusedView == self {
coordinator.addCoordinatedAnimations({ () -> Void in
self.layer.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.2).CGColor
}, completion: nil)
} else if context.previouslyFocusedView == self {
coordinator.addCoordinatedAnimations({ () -> Void in
self.layer.backgroundColor = UIColor.clearColor().CGColor
}, completion: nil)
}
}

Swift 3:

override var canBecomeFocused: Bool {
return true
}

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
if context.nextFocusedView == self {
coordinator.addCoordinatedAnimations({ () -> Void in
self.layer.backgroundColor = UIColor.blue.withAlphaComponent(0.2).cgColor
}, completion: nil)

} else if context.previouslyFocusedView == self {
coordinator.addCoordinatedAnimations({ () -> Void in
self.layer.backgroundColor = UIColor.clear.cgColor
}, completion: nil)
}
}

How to move focus at button in collectionview cell in tvos application?

I am able to solve this problem by adding below methods in collectionViewCell subclass.

In CollectionViewCell Subclass

override var preferredFocusEnvironments: [UIFocusEnvironment]{
// Condition
}
override func shouldUpdateFocus(in context: UIFocusUpdateContext) -> Bool {
// Condition
}
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
// Condition
}

you can see more at this link:
enter link description here.

Apple TV – How to only change focus when Menu button is tapped

Eventually, I noticed overriding pressesEnded won't cancel the Menu button press for the first time after launching the app. Using UITapGestureRecognizer on the other hand does:

    let menuTapGestureRegocnizer = UITapGestureRecognizer(target: self, action: "menuTapped")
menuTapGestureRegocnizer.allowedPressTypes = [UIPressType.Menu.rawValue]
view.addGestureRecognizer(menuTapGestureRegocnizer)

How to set initial focus to a button in a viewController from a UITabBarController?

Solution 1.
I think what you can do :

1.Subclass or Customize TabBarController and set
set first launch = true in viewdidload of tabController

> override weak var preferredFocusedView: UIView? {   if
> (self.firstLaunch) {
> self.firstLaunch = false;
> return self.selectedViewController!.preferredFocusedView; } else {
> let view = super.preferredFocusedView
> return view; }

Hope it helps.It worked for me though

Your focus engine takes the rootView controller as TabBarController and then asks for preferred focus view which is returned as UITabBarItem we need to unfocus that subclassing the class and returning canBecomeFocus to NO.

if you want to change the focus of the element in the firtsViewController then you can overrirde func preferredFocusView and reutrn the view you want to be focussed else preferredFocusView

Solution 2. Just set the root View controller as the HomeViewController without the TabBarController and embed the TabBarController from the second page. This is because anyways you don't need to use TabBar on the first page why need to take care of its focus.

TabBar is set to hidden if you hide from the AppDelegate or the view which is loaded but it has the focus.



Related Topics



Leave a reply



Submit