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,

Accepted Answer

The focus engine will automatically search for focusable views geometrically in the direction a user moves. Most of the standard UIKit controls are focusable so if you have 5 UIButtons lined up horizontally focus should move between then when the user taps or swipes left/right on the Siri Remote.

Thank you for your answer, but should I see something graphicly change on the screen or is it my job to get the focused object in a delegate if there is one and change the design ?

Yes, the buttons should turn white and get larger to show their state. The focus engine should automatically select a focusable view when the app launches. Alternatively you can implement -preferredFocusView in your view controller to let the engine know which button you'd like focused.


What does your view hierarchy look like?

It's strange my button doesn't change.


I had to use this methode and change the color and size of the focused button.

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context
       withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator


The other strange thing is when I add a button on a view the UIPressTypeSelect gesture don't work anymore, as soon as I remove the view with the buttons on it and get back to a view without any buttons then the UIPressTypeSelect work back. Any idea why I get this behavior ?


I have read that it should automatically send to the target of the UIButton when we press the touch button (When the UIButton is selected) but it doesn't do anything on my side.

Most of the standard UIKit controls will automatically handle their own focus appearance. Some allow a way to opt out, for instance if you alloc/init a UIButton instead of using the .System type.

Ok thanks,


And do you know why the UIPressTypeSelect (UITapGestureRecognizer) stop working as soon as I put a UIButton on the view ? The selected button doesn't seem to do anything and as soon as I remove all focusable elements it work back again.

I tried to create a UIButtonTypeSystem, like you said it has the animation and when I click on it it also animate, but still nothing is happening. I correctly set the target like that :

[thebutton addTarget:self action:@selector(thetarget:) forControlEvents:UIControlEventTouchUpInside];


Should I do something else ?

I think your problem is that you're specifying UIControlEventTouchUpInside. On tvOS, the UIButton responds to a Press, not a Touch. Try the new UIControlEventPrimaryActionTriggered control event, which we added specifically to make this more straightforward. (UIControlEventPrimaryActionTriggered will also work on iOS).


The distinction between a Touch and a Press is that a Touch happens when a finger is resting on the trackpad, just as if you were touching the screen of an iOS device. A Press happens when the user clicks the button on the remote.

It's working 😉 Thank you!

If I have a grid and the user is focused on an item on the far right side then tries to move right once more, is it possible to have focus move to the first item in the next line in the grid?

I suppose you could do this with a focus guide, but it would not map well to the way a user expects the focus engine to work. It is best to only move focus in the direction requested by the user, otherwise it becomes very unpredictable and confusing.


Please see the tvOS HIG section on Focus and Selection for some additional information.

I am having a similar issue but this great discussion doesnt seem to be solving it for me.


I am using storyboard and have view which contains a background UIImageView and two UIButtons. The buttons are Custom type and each have an image specified as well as a tag. I also have the following function:


    @IBAction func touchItem(sender : UIButton) {
        print(sender.tag)
    }


which is called when the button's Primary Action is Triggered as set up in the Sent Events panel. I was hoping that this would let me confirm that the buttons are working properly. I will not have my dev kit hardware until tomorrow and that may resolve some of this but for the moment I just dont understand what is going on.

Firstly, when I attempt to move the focus between the buttons, no visual indication of which has focus is being displayed. Am I understanding correctly that this is not possible if you are not using a System type? If that is the case, what is the recommended way to make a visual update when a UIButton receives focus?


Secondly, I am seeing the tag value printed when I try to use the simulator's remote but I dont understand why and what is actually causing my function to be called. I am Option clicking and dragging in the ssimulator remote window but it seems I am randomly seeing the tags from both buttons in my console. I thought that my actionwould just move focus. It feels like the 'Primary Action is Trigggered' event is firing when the button gets focus as I dont think I am initiating a button press of any sort. I am hoping that using my ATV hardware will make it a little clearer but if it looks to anyone like I have done something wrong, I would greatly appreciate any input.


Thanks!

In order to handle a custom focus appearance implement the following method in your button subclass.


- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
    if (context.nextFocusedView == self) {
        // handle focus appearance changes
    }
    else {
        // handle unfocused appearance changes
    }
}

Thank you!

I'd like to keep the buttonType as System to keep most of the focus behavior goodies, but change the background color behavior particularly white when focused. Is that possible? (And if so how?)


Thanks,

Mark

This is not possible, please file a bug report at https://bugreport.apple.com for features you'd like to see.

Buttons Focus
 
 
Q