【问题标题】:UIBarButtonItem is disabled, but has normal colorUIBarButtonItem 被禁用,但颜色正常
【发布时间】:2015-03-20 11:48:10
【问题描述】:

我遇到了UIBarButtonItem 的问题。我使用外观代理设置状态NormalDisabled 的颜色,并在UIViewControllerviewDidLoad 方法中执行此操作。然而,按钮获得Normal 颜色,即使它被禁用(它肯定被禁用,因为没有调用IBAction 方法)。

问题与text color of disabled uibarbuttonitem is always the color of the normal state 类似,但是,此处发布的解决方案对我不起作用。

我的应用适用于 iOS 8.2,我使用的是 Xcode 6.2

有什么想法吗?

编辑: 我不确定这是否有助于找到解决方案,但是当我使用 initWithImage: 而不是 initWithTitle: 创建按钮时,一切似乎都运行良好。这可能是 Apple 的错误吗?

【问题讨论】:

  • 您能否将您的代码发布到您使用外观代理设置状态颜色的位置?
  • 我以编程方式创建了按钮 viewDidLoad,我在我的导航栏的 rightBarButtonItem 添加了 is,然后我调用外观代理。

标签: ios objective-c uibarbuttonitem


【解决方案1】:

斯威夫特 4

如果有人在寻找如何快速更改 barbuttonitem 禁用状态的外观。给你。

barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.lightGray], for: .disabled)

【讨论】:

  • 请解释你的答案。
  • UIColor.init(white: 0.8, alpha: 1.0) 更接近于默认的禁用颜色。谢谢。
【解决方案2】:

请关注code

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem * btnTemp = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(btnDone_Click:)];
    [btnTemp setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f]} forState:UIControlStateNormal];

    [btnTemp setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor], NSFontAttributeName:[UIFont systemFontOfSize:16.0f]} forState:UIControlStateDisabled];

    [self.navigationItem setRightBarButtonItem:btnTemp];

}

- (void)btnDone_Click : (id)sender {

    UIBarButtonItem * button = (UIBarButtonItem *)sender;
    [button setEnabled:FALSE];
    [self performSelector:@selector(enableButton:) withObject:sender afterDelay:2.0f];


}

- (void)enableButton : (id)sender {
    UIBarButtonItem * button = (UIBarButtonItem *)sender;
    [button setEnabled:TRUE];
}

【讨论】:

  • 以上代码我确定了您给定状态下的按钮颜色。点击后它将禁用并在 2 秒后再次启用,以便您再次检查...
  • 我的按钮最初是禁用的,所以这对我不起作用。问题不在于按钮的状态,状态是正确的,因为从未调用过 onClick 选择器。只是颜色的问题,即按钮有启用的颜色,但无法点击。
【解决方案3】:

所以我终于设法让它正常工作,问题是我设置了 UIBarButtonItems 的颜色两次,一次使用 [navBar setTintColor:] 一次使用外观代理。只保留外观代理即可解决问题。

【讨论】:

  • 删除代码来解决问题总是比添加更多代码来解决问题要好,很高兴你找到了这个解决方案:)
【解决方案4】:

您可能已经为.Normal 状态设置了条形按钮项目标题文本属性,并且还需要为.Disabled 状态设置它。

有两种方法可以解决这个问题,一种是在栏按钮项实例上设置标题文本属性,另一种是使用外观代理。

单个实例:

saveButton.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], forState: .Disabled)

外观代理:

UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([MyNavigationController.self]).setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], forState: .Disabled)

【讨论】:

  • 这很好,但是像UIButton 这样的其他控件会自动计算禁用状态的色调颜色,作为正常颜色的较浅版本。如何使用外观代理并在UIButtonUIBarButtonItem 之间设置一致禁用标题颜色? UIButton的外观代理不响应setTitleTextAttributes...
【解决方案5】:

这更新了 Swift 4.0 的答案:

barButtonItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.gray], for: UIControlState.disabled)

这显示了一个橙色的说明性示例,用于禁用白色 barTintColor:

barTintColor = .white    
cancelButton.isEnabled = false
cancelButton.setTitleTextAttributes(
    [NSAttributedStringKey.foregroundColor: UIColor.orange], 
    for: UIControlState.disabled)

【讨论】:

    【解决方案6】:

    这也是我在 iOS 版本 10.* 上运行时遇到的问题。设置按钮的前景色为我解决了这个问题。

    self.saveButton.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.gray], for: UIControlState.disabled)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      相关资源
      最近更新 更多