由于tvOS中没有UISwitch
,我使用UIButton
来实现简单的开/关切换。我已为UIControlStateNormal
和UIControlStateSelected
设置按钮标题文字,以指示按钮的开/关状态,但新UIControlStateFocused
现在通过设置只要按钮处于焦点,标题文本就与默认状态相同。这意味着当按钮为"开"时,无论何时获得焦点,其标题都会变为"关"。
我发现绕过它的唯一方法是在按钮处理程序中明确设置焦点状态的标题,如下所示。
- (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];
}
}
这对我来说非常讨厌,尤其是因为在UISwitch缺席的情况下可能会有很多这样的事情发生。还有更好的方法吗?
答案 0 :(得分:0)
您是否尝试为UIControlStateFocused | UIControlStateSelected
设置标题?这是一个有点领域,所以应该可以将它们组合起来。