我的构建目标设置为IOS5,这是我理解的UIButton.tintColor介绍...
我在视图控制器的viewDidLoad中有这个
[timelineButton setTintColor:[UIColor blackColor]];
[timelineButton setTitle:@"test" forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
文本更改正常,但按钮不是黑色?
谢谢!
Eva*_*ski 57
根据文件:
此属性对所有按钮类型无效.
您需要将切换buttonType到另一个其中之一.(遗憾的是,文档未指定哪些特定按钮类型支持此属性.)
它为您突出显示的状态颜色着色.当您点击/单击UIButton时,只要您按住UIButton上的点击/单击,就会出现用tintColor指定的颜色.
resetButton.tintColor = [UIColor colorWithRed:0.764 green:1.000 blue:0.000 alpha:1.000];
Run Code Online (Sandbox Code Playgroud)
正常状态下按钮为白色.但是,如果我按下按钮,颜色会变成红色,但只有这样.
如果您需要更改按钮,那么它在UIControlStateNormal中看起来像红色或蓝色
在Interface Builder中将UIButtonType更改为UIButtonTypeCustom或以编程方式将其更改为
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
Run Code Online (Sandbox Code Playgroud)
自己更改属性并重新创建圆角
resetButton.backgroundColor = [UIColor redColor];
resetButton.layer.borderColor = [UIColor blackColor].CGColor;
resetButton.layer.borderWidth = 0.5f;
resetButton.layer.cornerRadius = 10.0f;
Run Code Online (Sandbox Code Playgroud)
如其他答案中所述,色调不适用于自定义按钮类型.确保明确声明按钮类型.不要只使用[UIButton alloc] init]
这将有效:
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mybutton setImage:[UIImage imageNamed:@"myImage"] forState:UIControlStateNormal];
mybutton.tintColor = [ODTheme blueTintColor];
Run Code Online (Sandbox Code Playgroud)
我发现必须遵循三件事。
1. 将图像渲染为Default
去Images.xcassets > Your Image > Show the Attributes inspector > Render As > Default
2. 确保您的按钮类型是系统
3. 现在更改按钮的tintColor。
完毕。