Buttons Focus

Hi everyone,


I am trying to understand the Focus system on tvOS but I still don't understand. I have a simple UIView and inside 5 UIButton. But nothing is happening, what am I supposed to do know ? I guess I have to give the Focus to the first button then the user will navigate between buttons like in the Settings menu of the Apple TV. But what should I do to do that, should I code everything myself or is the Focus implementation do it automatically. I can't fin any peace of code or explanation for this case. Have you any idea ?


Thank you,

So I'm running into a similar problem but on an SKScene where I'm planning to have home scene for my game, which has two buttons. I created a function to return the buttons to add to the scene and I tried the following code, and I can see that it changes colors as I planned, between green and yellow based on what seems to be the currently focused button. BUT, pressing the "Select" on the TV remote doesn't do anything so the buttonAction function is not called AND I even tested the values of the buttons to see if they're "focusable" but that returns false. See below:


    func createAndReturnUIButton(pImageName: String, pNodeName: String, pFrame: CGRect) -> UIButton {
     
        let btnNode = UIButton(frame:pFrame)
        btnNode.backgroundColor = UIColor.clearColor()
        btnNode.setTitle(pNodeName, forState: UIControlState.Normal)
        btnNode.setTitleColor(
            UIColor.greenColor(), forState: UIControlState.Normal)
        btnNode.setTitleColor(
            UIColor.yellowColor(), forState: UIControlState.Focused)
     
        btnNode.addTarget(self, action: "buttonAction",
                forControlEvents:  UIControlEvents.TouchDown)
        self.view!.addSubview(btnNode)
     
        return btnNode
    }





        print("UIScreen.mainScreen().focusedView: \(UIScreen.mainScreen().focusedView)\nbtnEasyStart.focused:\(btnEasyStart.focused)\nbtnHardStart.focused:\(btnHardStart.focused)")


focusedView returns nil while the focused value of both buttons returns false.

Any idea what's wrong? Input is appreciated.

Thanks!

See pschneider's post Sept 10, above...

So guys i was having the same issue creating uibuttons programatically, the only way to auto use the focus system is using UIButtonType.System , now.. when i create my button like that. now im not seeing the labels.. any help ? Focus works.. but no titles now..


var i = 1.00
        var tag = 1
        for letter in self.alfabetico {
            /
            let  b = i++ * 30.00
            let tagtag = tag++
          
            print(" hola \(b)")
          
            let butletter  = UIButton(type: UIButtonType.System)
           /
            butletter.setTitle("\(letter)", forState: .Normal)
            butletter.setTitleColor(UIColor.redColor(), forState: .Normal)
            butletter.setTitleColor(UIColor.redColor(), forState: UIControlState.Selected)
           butletter.titleLabel!.font = UIFont(name: "Helvetica Neue", size: 5)
          
          
            var framewidth = CGFloat(40)
            var xfloat = CGFloat(b)
            if(letter == "ALL") {
                framewidth = CGFloat(60)
                xfloat = CGFloat(b + 30.00)
            }
            butletter.frame = CGRectMake(xfloat, 0, framewidth, 40)
          
            butletter.userInteractionEnabled = true
            butletter.tag = tagtag
            butletter.addTarget(self, action: "pressed:", forControlEvents: .PrimaryActionTriggered)
            / butletter.addTarget(self, action: "pressed2:", forControlEvents: .TouchUpOutside)
            */
            self.ButtonsScrollView.addSubview(butletter)
            print("Agregando button \(butletter)")
          
        }

You can use a System type button and build the title into the image (for each ControlState as needed) using

attString.drawWithRect(drawRect, options: .UsesLineFragmentOrigin, context: nil)


It's a PITA, but it works.

Answer : i had to give more space to the frame of the but.. 😉 .. text wasnt showing..

Hello, are custom UIButton focusable? I have changed a system button to a custom button, but it seems that I am not able to focus it in the simulator

Buttons Focus
 
 
Q