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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19738940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:43:41  来源:igfitidea点击:

set title color for UIButton when highlighted iOS 7

iosiphoneobjective-cuibuttonios7

提问by Adam Langsner

I have the following code in a viewController, all the outlets and action are hooked up correctly. the WHITEand PURPLEare UIColors that I've defined constants for. I've also set the UIWindow's tintColorto PURPLEand that propagates down to the button.

我在 viewController 中有以下代码,所有插座和动作都正确连接。的WHITEPURPLE是UIColors,我已经为定义的常量。我还将UIWindow's设置为tintColorPURPLE然后向下传播到按钮。

- (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? I heard if I do that I won't get the advantages of iOS 7 doing its magic with tintColor. Not sure what's the correct way to go about this. Thanks in advance.

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

normalStatehighlightedState

正常状态高亮状态

回答by David Wong

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. The highlighted button state defaults to its own implementation which is a lighter version if your tintcolor.

您需要将按钮样式设置为自定义。UIButton 系统风格做了很多你无法改变的事情。突出显示的按钮状态默认为它自己的实现,如果您的 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.

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

回答by Seema Sharma

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 红色而不是 btnCustom.selected = NO 并且如果你想要带有白色标题颜色的按钮而不是 make btnCustom.selected = YES