简体   繁体   English

突出显示iOS 7时,为UIButton设置标题颜色

[英]set title color for UIButton when highlighted iOS 7

I have the following code in a viewController, all the outlets and action are hooked up correctly. 我在viewController中有以下代码,所有的出口和动作都正确连接。 the WHITE and PURPLE are UIColors that I've defined constants for. WHITEPURPLE是我为其定义常量的UIColors。 I've also set the UIWindow 's tintColor to PURPLE and that propagates down to the button. 我还将UIWindowtintColorPURPLE ,然后传播到按钮。

- (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;
}

When I click the button the text doesn't go white like i told it to in viewDidLoad 当我单击按钮时,文本不会像我在viewDidLoad告诉它那样变白

here's some screenshots that I could've cropped better! 这里有一些截图,我可以更好地裁剪! As you can see in the highlighted state it's not white but like a white and purple mix. 正如您在突出显示的状态中所看到的那样,它不是白色,而是像白色和紫色混合。 Will I need to use UIButtonTypeCustom ? 我需要使用UIButtonTypeCustom吗? I heard if I do that I won't get the advantages of iOS 7 doing its magic with tintColor . 我听说如果我这样做,我将无法通过tintColor获得iOS 7的优势。 Not sure what's the correct way to go about this. 不确定这是怎样的正确方法。 Thanks in advance. 提前致谢。

normalStatehighlightedState

Shouldn't you be using UIControlStateSelected? 你不应该使用UIControlStateSelected吗?

Okay, I just tried this out myself it don't want to work well. 好吧,我只是自己试了一下它不想工作得很好。

You need to set your button style to Custom. 您需要将按钮样式设置为自定义。 The UIButton system style does a lot of things that you can't change. UIButton系统风格做了许多你无法改变的事情。 The highlighted button state defaults to its own implementation which is a lighter version if your tintcolor. 突出显示的按钮状态默认为它自己的实现,如果你的tintcolor,它是一个更轻的版本。

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

That should get you the white text background. 这应该会得到白色文本背景。

Hope this is the answer of your question or someone still finding difficulty to set title color when button selected. 希望这是你的问题的答案,或者当选择按钮时仍然难以设置标题颜色的人。

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

when you want button title coloe red than make btnCustom.selected = NO and if you want button with white title color than make btnCustom.selected = YES 当你想要按钮标题coloe red而不是make btnCustom.selected = NO并且如果你想要按钮的白色标题颜色比make btnCustom.selected = YES

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM