设置突出显示iOS 7时UIButton的标题颜色
我在viewController中有以下代码,所有的插座和动作都正确连接。白色和紫色是我定义的常量。我还将设置突出显示iOS 7时UIButton的标题颜色,ios,iphone,objective-c,uibutton,ios7,Ios,Iphone,Objective C,Uibutton,Ios7,我在viewController中有以下代码,所有的插座和动作都正确连接。白色和紫色是我定义的常量。我还将ui窗口的tintColor设置为PURPLE,并向下传播到按钮 - (void)viewDidLoad { [super viewDidLoad]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; button.backgroundColor = WHIT
ui窗口的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
?我听说,如果我这样做,我就不会获得iOS 7的优势,因为它通过tintColor
发挥魔力。我不知道该怎么做。提前谢谢
您不应该使用UIControlStateSelected吗
好吧,我自己也试过了,不想好用
您需要将按钮样式设置为“自定义”。UIButton系统样式做了很多您无法更改的事情。突出显示的按钮状态默认为其自己的实现,如果您选择淡色,则该实现是较轻的版本
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];
当您希望按钮标题颜色为红色而不是make btnCustom.selected=否,如果您希望按钮标题颜色为白色而不是make btnCustom.selected=是您是否尝试了-setTitleShadowColor?您的问题回答了我的问题,您不需要使用UIControlStateSelected
,因为UIButton
没有该状态UIControlStateSelected
用于UISegmentedControl
@greenisus的选中选项卡。请注意,UIButton、UISegmentedControl和其他UIKit类继承了选中状态的UIControl。界面生成器和代码都支持使用UIControlStateSelected UIControlStateDisabled等进行的可视更改。UIControl的selected属性的伟大之处在于从按钮调用isSelected并允许按钮处理布尔属性。将按钮样式设置为Custom对我来说很有用!谢谢看起来这只是三年前答案的副本。