【问题标题】:UIButton in tvOS: focused state interferes with texttvOS 中的 UIButton:聚焦状态会干扰文本
【发布时间】:2016-01-03 15:48:42
【问题描述】:

由于 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 缺席的情况下可能会发生很多这样的事情。有没有更好的办法?

【问题讨论】:

    标签: objective-c uibutton uiswitch tvos


    【解决方案1】:

    您是否尝试为 UIControlStateFocused | UIControlStateSelected 设置标题?这是一个位域,所以应该可以将它们组合起来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      相关资源
      最近更新 更多