【问题标题】:How to set same title color of UIButton for different states?如何为不同的状态设置 UIButton 的相同标题颜色?
【发布时间】:2015-07-07 08:24:44
【问题描述】:

我创建了一个UIButton 的实例,并希望为Normal 状态、Selected 状态和Highlighted 状态设置标题颜色。我这样做是因为-

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState: UIControlStateSelected];
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

使用上面的代码,当我构建和运行应用程序时,文本按预期显示为红色。

但是当我将一个语句中所有状态的按钮标题颜色设置为 -

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted];

构建并运行应用程序...

问题

按钮上的文字显示为白色。

这(上述语句)不是设置UIButton 的标题颜色的正确方法吗?我需要在三个不同的语句中设置UIButton 的标题颜色吗?

感谢任何建议/帮助!

非常感谢。

【问题讨论】:

  • 来自您的尝试:In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
  • 你的问题是这样的:stackoverflow.com/questions/4370466/…

标签: ios objective-c cocoa-touch uibutton uicolor


【解决方案1】:

只为正常状态设置titleColor

 [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

 button.tintColor = [UIColor redColor];

【讨论】:

  • 谢谢,但为什么我不能在一个语句中为所有状态设置红色?
  • setTitleColor 不评估您的 or 条件,这就是您面临问题的原因
  • 但通常属性都是这样设置的
  • 还有为什么要设置 tint color,它的作用是什么?
  • 为其他州设置颜色怎么样?
【解决方案2】:

UIControlState 定义为

typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal       = 0,
UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
UIControlStateDisabled     = 1 << 1,
UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
};

当你写作时

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted];

这实际上意味着您正在为状态 0x00000101 设置红色。按钮永远不会处于这种状态,因为它是未定义的。所以行为也是未定义的。

【讨论】:

  • 您指出了正确的方向。如果您留下详细的解释会更好,因为没有很多人了解位掩码的工作原理。 :)
【解决方案3】:

您需要了解位掩码的工作原理。 Merlin 指出了正确的方向,但他实际上并没有给出解释。

typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal       = 0,
    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
    UIControlStateDisabled     = 1 << 1,
    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
};

UIControlStateNormal 是默认状态。它并没有真正使用位掩码。 ENUM 中的状态 UIControlStateHighlightedUIControlStateDisabledUIControlStateSelected 正在使用位掩码,因此可以按照您在 OP 中所做的方式使用。

例如,看看ENUMUIUserNotificationType

typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) {
    UIUserNotificationTypeNone    = 0,      // the application may not present any UI upon a notification being received
    UIUserNotificationTypeBadge   = 1 << 0, // the application may badge its icon upon a notification being received
    UIUserNotificationTypeSound   = 1 << 1, // the application may play a sound upon a notification being received
    UIUserNotificationTypeAlert   = 1 << 2, // the application may display an alert upon a notification being received
} NS_ENUM_AVAILABLE_IOS(8_0);

UIUserNotificationTypeNone 不是位掩码。您不会同时注册 Sound、badge 和 none 类型的通知。没有,其他的必须是互斥的(XOR 之类的)。

同样适用于UIControlStateUIControlStateNormal 不得与其他状态一起使用。您可以以任意组合使用位掩码值,但如果您引入单个非位掩码值,结果将是您意想不到的结果。对于您的具体情况,Merlin 给出了将非位掩码值与其他位掩码一起使用的确切结果。

简而言之,如果您使用UIControlStateNormal,请同时设置tintColor。否则,仅使用位掩码值。

【讨论】:

  • 感谢您的详细回答,真的很有帮助。
【解决方案4】:

你也可以用故事板来做这些。

从状态配置属性中,您可以选择您想要的任何状态并相应地设置不同的属性。

【讨论】:

    猜你喜欢
    • 2014-09-29
    • 1970-01-01
    • 2011-12-30
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多