【问题标题】:set title color for UIButton when highlighted iOS 7突出显示iOS 7时为UIButton设置标题颜色
【发布时间】:2013-11-13 08:56:42
【问题描述】:

我在 viewController 中有以下代码,所有的出口和动作都正确连接。 WHITEPURPLE 是我为其定义了常量的 UIColors。我还将UIWindowtintColor 设置为PURPLE,并向下传播到按钮。

- (void)viewDidLoad {
    [super viewDidLoad];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    button.backgroundColor = WHITE;
    button.layer.borderWidth = 1.0;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 5.0;
    button.layer.borderColor = PURPLE.CGColor;
}

-(IBAction) buttonTouchDown:(id)sender {
    button.backgroundColor = PURPLE;
    button.layer.borderColor = WHITE.CGColor;
}

-(IBAction) buttonTouchUpOutside:(id)sender {
    button.backgroundColor = WHITE;
    button.layer.borderColor = PURPLE.CGColor;
}

-(IBAction) buttonTouchUpInside:(id)sender {
    button.backgroundColor = WHITE;
    button.layer.borderColor = PURPLE.CGColor;
}

当我点击按钮时,文本不会像我在 viewDidLoad 中告诉它的那样变白

这里有一些我可以剪得更好的截图! 正如您在突出显示的状态中看到的那样,它不是白色的,而是白色和紫色的混合。 我需要使用UIButtonTypeCustom 吗?我听说如果我这样做,我将无法获得 iOS 7 使用 tintColor 带来的魔力。不确定解决此问题的正确方法是什么。提前致谢。

【问题讨论】:

  • 你试过-setTitleShadowColor吗?
  • 你的问题回答了我的问题

标签: ios iphone objective-c uibutton ios7


【解决方案1】:

希望这是您问题的答案,或者有人在选择按钮时仍然难以设置标题颜色。

[btnCustom setTitleColor:[UIColor whiteColor] forState: UIControlStateSelected];
[btnCustom setTitleColor:[UIColor redColor] forState: UIControlStateNormal];

如果您希望按钮标题颜色为红色而不是使 btnCustom.selected = NO 并且如果您希望按钮标题颜色为白色而不是使 btnCustom.selected = YES

【讨论】:

  • 看起来这只是 3 年前答案的副本。
【解决方案2】:

你不应该使用 UIControlStateSelected 吗?

好的,我只是自己尝试了一下,它不想很好地工作。

您需要将按钮样式设置为自定义。 UIButton 系统风格做了很多你无法改变的事情。突出显示的按钮状态默认为其自己的实现,如果您的 tintcolor 是较轻的版本。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

这应该会让你得到白色的文本背景。

【讨论】:

  • 您不需要使用UIControlStateSelected,因为UIButton 没有该状态。 UIControlStateSelected 用于 UISegmentedControl 的选定选项卡之类的内容。
  • @greenisus 请注意,UIButton、UISegmentedControl 和其他 UIKit 类继承了具有选择状态的 UIControl。 Interface Builder 和代码都支持使用 UIControlStateSelected UIControlStateDisabled 等进行的视觉更改。UIControl 的 selected 属性的好处是从按钮调用 isSelected 并允许按钮处理布尔属性。
  • 将按钮样式设置为自定义对我有用!谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-05-29
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 2011-09-15
相关资源
最近更新 更多