[英]set title color for UIButton when highlighted iOS 7
我在viewController中有以下代码,所有的出口和动作都正确连接。 WHITE
和PURPLE
是我为其定义常量的UIColors。 我还将UIWindow
的tintColor
为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
吗? 我听说如果我这样做,我将无法通过tintColor
获得iOS 7的优势。 不确定这是怎样的正确方法。 提前致谢。
你不应该使用UIControlStateSelected吗?
好吧,我只是自己试了一下它不想工作得很好。
您需要将按钮样式设置为自定义。 UIButton系统风格做了许多你无法改变的事情。 突出显示的按钮状态默认为它自己的实现,如果你的tintcolor,它是一个更轻的版本。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
这应该会得到白色文本背景。
希望这是你的问题的答案,或者当选择按钮时仍然难以设置标题颜色的人。
[btnCustom setTitleColor:[UIColor whiteColor] forState: UIControlStateSelected];
[btnCustom setTitleColor:[UIColor redColor] forState: UIControlStateNormal];
当你想要按钮标题coloe red而不是make btnCustom.selected = NO并且如果你想要按钮的白色标题颜色比make btnCustom.selected = YES
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.