Remove focus from UIButton in tvOS

1.3k Views Asked by At

So I'm writing my first tvOS App (Objective C) and am having some fun with the "Focus Engine". My app is a 2 page app with a Tab Bar controller, on the main page I have a few UIButtons. On app startup if I hide the tab bar, the buttons look like I am wanting them to with them all deselected, when I swipe down one of the UIButtons obviously gets focus, and I can swipe between my various buttons, and after a specified amount of inactivity time I want it to go back to them all being unfocussed.

I start (and reset) a NSTimer when each UIButton gets focus and my intention is to remove the UIButton focus after say 10 seconds (there is a good reason for this, and it makes sense in my app / ui).

I've tried issuing a "UIButton resignFirstResponder" I've also tried to move focus back to the hidden TabBar, I even tried "preferredFocusEnvironments" but I cannot get the button highlight to come away. I also tried cycling though the buttons setting them all to "userInteractionEnabled = NO" then back again but the button retains focus. I have log lines showing the timer starting and it triggering my un-focus method when it expires, but no matter what I put in there I can't seem to get the focus to disappear.

Any ideas on how to drop the focus from a UIButton, I think part of the problem is I don't want to move it to somewhere else. I want to remove all button focus which I guess is an unusual thing to do.

Thanks in advance.

Plasma

1

There are 1 best solutions below

0
On BEST ANSWER

I discovered a way to do it, its a bit primitive but it works and achieves the desired effect.

When the idle timer expires I create a UIButton (Custom Type) at 0,0 that is 1px high and the width of the screen. I then tell the view it needs a focus update, and to update the focus. This takes the focus from any of the main buttons and up to my 1px high button along the top of the screen.

[focusButton removeFromSuperview];
CGRect frame = CGRectMake(0,0,(self.view.frame.size.width),1);
focusButton = [UIButton buttonWithType:UIButtonTypeCustom];
focusButton.frame = frame;
focusButton.tag = 99;
[self.view addSubview:focusButton];
[self.view setNeedsFocusUpdate];
[self.view updateFocusIfNeeded];

I then use 'didUpdateFocusInContext' to know when my 1px button has been given focus and set the button to disabled.

if(context.nextFocusedView.tag == 99){
        NSLog(@"Focus Button Has Focus");
        focusButton.enabled = NO;
}

This leaves the focus on that button which allows someone to swipe down to get to the main buttons, or up to get to the Tab Bar, once they swipe off it the 1px button is no longer selectable because is not enabled! I had to use Custom button type because system button type showed a white line over the top of the screen and tainted my labels.

Plasma