Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tvOS: Focus first button as preferred focus view when buttons are in horizontal order

I have been trying to focus the first button but focus engine takes the third button right below the tab bar as the first item to be focussed. I tried with preferred focus view but found that when i place the buttons in vertical order then preferred takes the preferred view to be focussed but when i placed all the buttons in horizontal plane it always takes the third button.The other approach i can think of if Focus Guide but i wonder how that will work in this scenario?

 override weak var preferredFocusedView: UIView? {
        get {
            return thrirdButton
        }
    }

It happens because focus engine takes the nearest possible focus element as 1st element as you can see from the picture attached.I have attached the context screenshot for the view controller. Any help or clue to solve this will be appreciated.

Focus path expected-Blue vs default-Red Context Path

like image 979
creative_rd Avatar asked Jan 23 '16 17:01

creative_rd


1 Answers

Solution : We need to add focus guide just above button 3 and redirect it to button one when the focus guide is focussed.

private var focusGuide = UIFocusGuide()
self.view.addLayoutGuide(focusGuide)

    self.focusGuide.bottomAnchor.constraintEqualToAnchor(self.button3.topAnchor).active = true
    self.focusGuide.leftAnchor.constraintEqualToAnchor(self.button3.leftAnchor).active = true
    // Width and height
    self.focusGuide.widthAnchor.constraintEqualToAnchor(self.button3.widthAnchor).active = true
    self.focusGuide.heightAnchor.constraintEqualToAnchor(self.button3.heightAnchor).active = true
    focusGuide.centerXAnchor.constraintEqualToAnchor(self.button3.centerXAnchor).active = true


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

        self.focusGuide.preferredFocusedView = button1
    }

Context for the focus guide shown in sky blue

like image 167
creative_rd Avatar answered Nov 04 '22 19:11

creative_rd