3

Since there's no UISwitch in tvOS, I'm using a UIButton to implement a simple On/Off toggle. I've set the button title text for UIControlStateNormal and UIControlStateSelected to indicate the button's on/off state, but the new UIControlStateFocused now interferes with this by setting the title text to be the same as the default state whenever the button is in focus. This means that when the button is "On", whenever it gets focus its title changes to "Off".

The only way I've found to get around it is to explicitly set the title for the focused state in the button handler as shown below.

- (void)viewDidLoad 
{
    [super viewDidLoad];
    // in reality these strings are setup in the storyboard
    [self.enabledButton setTitle:@"Off" forState:UIControlStateNormal];
    [self.enabledButton setTitle:@"On" forState:UIControlStateSelected];
    // ensure the text shows up in focused state
    [self.enabledButton setTitleColor:[UIColor blackColor] forState:UIControlStateFocused];
}

- (IBAction)toggleStateForEnabledButton:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
    if (button.selected)
    {
        [button setTitle:[button titleForState:UIControlStateSelected] forState:UIControlStateFocused];
    }
    else
    {
        [button setTitle:[button titleForState:UIControlStateNormal] forState:UIControlStateFocused];
    }
}

This feels very hackish to me, esp. since there might be lot of this going on in UISwitch's absence. Is there a better way?

Echelon
  • 7,306
  • 1
  • 36
  • 34

1 Answers1

0

Have you tried setting a title for UIControlStateFocused | UIControlStateSelected ? It's a bit field, so should be possible to combine them.

uliwitness
  • 8,532
  • 36
  • 58