ios 为什么我的 UIButton.tintColor 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11066008/
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
Why is my UIButton.tintColor not working?
提问by sayguh
My build target is set for IOS5 which is where I understand UIButton.tintColor
was introduced...
我的构建目标是为 IOS5 设置的,这是我所了解的UIButton.tintColor
被引入的地方...
I have this in my viewDidLoad for the View Controller
我在视图控制器的 viewDidLoad 中有这个
[timelineButton setTintColor:[UIColor blackColor]];
[timelineButton setTitle:@"test" forState:UIControlStateNormal];
The text changes properly but the button isn't black?
文本正确更改但按钮不是黑色的?
Thanks!
谢谢!
回答by Evan Mulawski
According to the documentation:
根据文档:
This property is not valid for all button types.
此属性并非对所有按钮类型都有效。
You need to switch your buttonType
to another one of these. (Unfortunately, the documentation does not specify which specific button types support this property.)
您需要切换buttonType
到其中的另一个。(不幸的是,文档没有指定支持此属性的特定按钮类型。)
回答by Jesse S.
Make sure your button "type" is set to System. If it is set to Custom it could be the reason the color of your button isn't changing. This is what happened to me and changing the type to System fixed it.
确保您的按钮“类型”设置为系统。如果它设置为自定义,则可能是按钮颜色没有改变的原因。这就是发生在我身上的事情,将类型更改为 System 修复了它。
回答by Binarian
It tint your highlighted State color. When you tap/click on the UIButton the color specified with tintColor appears as long as you hold the tap/click on the UIButton.
它为您突出显示的状态颜色着色。当您点击/单击 UIButton 时,只要您按住 UIButton 上的点击/单击,就会出现 tintColor 指定的颜色。
resetButton.tintColor = [UIColor colorWithRed:0.764 green:1.000 blue:0.000 alpha:1.000];
The button is white in normal state. But if I tap on the button the color turns red, but only then.
按钮在正常状态下为白色。但是如果我点击按钮,颜色会变成红色,但只有那时。
IF you need to change the button so it looks like a red or blue one in the UIControlStateNormal then
如果您需要更改按钮,使其看起来像 UIControlStateNormal 中的红色或蓝色按钮,则
Change the UIButtonType to UIButtonTypeCustom in Interface Builder or programmatically with
在 Interface Builder 中或以编程方式将 UIButtonType 更改为 UIButtonTypeCustom
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
Change the attributes on your own and recreate the rounded corners
自行更改属性并重新创建圆角
resetButton.backgroundColor = [UIColor redColor];
resetButton.layer.borderColor = [UIColor blackColor].CGColor;
resetButton.layer.borderWidth = 0.5f;
resetButton.layer.cornerRadius = 10.0f;
回答by Robert Wagstaff
As stated in other answers tint does not work for Custom Button types. Make sure you explicitly declare the button type. Do not just use [UIButton alloc] init]
正如其他答案中所述, tint 不适用于自定义按钮类型。确保您明确声明按钮类型。不要只使用 [UIButton alloc] init]
This will work:
这将起作用:
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mybutton setImage:[UIImage imageNamed:@"myImage"] forState:UIControlStateNormal];
mybutton.tintColor = [ODTheme blueTintColor];
回答by alexqinbj
Today, I also meet this problem. I use delegate to solve it.
今天,我也遇到了这个问题。我使用委托来解决它。
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(buttonPressReset:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
-(void)buttonPress:(id)sender{
UIButton* button = (UIButton*)sender;
[button setBackgroundColor:[UIColor greenColor]];
NSLog(@"buttonPressed");
}
-(void)buttonPressReset:(id)sender{
UIButton* button = (UIButton*)sender;
[button setBackgroundColor:[UIColor redColor]];
NSLog(@"buttonPressReset");
}
回答by Zaid Pathan
I found 3 things must be followed.
我发现必须遵循 3 件事。
1. Render image as Default
1. 将图像渲染为 Default
Go to Images.xcassets > Your Image > Show the Attributes inspector > Render As > Default
去 Images.xcassets > Your Image > Show the Attributes inspector > Render As > Default
2. Make sure your button type is System
2. 确保你的按钮类型是 System
3. Now change button's tintColor.
3. 现在更改按钮的 tintColor。
Done.
完毕。
回答by Zverusha
Should try this method:
应该试试这个方法:
- (void)setTitleColor:(UIColor *)color
forState:(UIControlState)state
回答by Bart?omiej Semańczyk
回答by Bhavesh Patel
If your UIButton type is system then and then only tint colour property is work. So first you need to set your button type to the system then apply tint colour for the specific state.
如果您的 UIButton 类型是 system 则只有 tint color 属性有效。因此,首先您需要将按钮类型设置为系统,然后为特定状态应用色调颜色。
let btn = UIButton.init(type: .system)
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
回答by rockdaswift
In iOS 13 you can tint the image and then set it to the button:
在 iOS 13 中,您可以为图像着色,然后将其设置为按钮:
let coloredImage = UIImage().withTintColor(UIColor.red)
UIButton().setImage(coloredImage, for: .normal)