为什么我的UIButton.tintColor不起作用?

时间:2012-06-16 18:29:23

标签: ios uibutton

我的构建目标是针对IOS5设置的,我理解UIButton.tintColor已经介绍了......

我在视图控制器的viewDidLoad中有这个

[timelineButton setTintColor:[UIColor blackColor]];
[timelineButton setTitle:@"test" forState:UIControlStateNormal];

文字更改正确,但按钮不是黑色?

谢谢!

13 个答案:

答案 0 :(得分:50)

根据documentation

  

此属性对所有按钮类型无效。

您需要将buttonType切换为另一个one of these。 (遗憾的是,文档未指定哪些特定按钮类型支持此属性。)

答案 1 :(得分:28)

确保您的按钮"键入"设置为系统。如果将其设置为“自定义”,则可能是按钮颜色未发生变化的原因。这就是我发生的事情并将类型更改为System修复它。

答案 2 :(得分:8)

它为突出显示的状态颜色着色。 当您点击/单击UIButton时,只要您按住UIButton上的点击/点击,就会出现用tintColor指定的颜色。

resetButton.tintColor = [UIColor colorWithRed:0.764 green:1.000 blue:0.000 alpha:1.000];

正常状态下按钮为白色。 但是,如果我按下按钮,颜色会变成红色,但只有这样。

如果您需要更改按钮,使其在UIControlStateNormal中看起来像红色或蓝色,那么

在Interface Builder中将UIButtonType更改为UIButtonTypeCustom,或使用

以编程方式更改
 UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];

自行更改属性并重新创建圆角

resetButton.backgroundColor = [UIColor redColor];
resetButton.layer.borderColor = [UIColor blackColor].CGColor;
resetButton.layer.borderWidth = 0.5f;
resetButton.layer.cornerRadius = 10.0f;

答案 3 :(得分:7)

如其他答案所述,色调不适用于自定义按钮类型。确保明确声明按钮类型。不要只使用[UIButton alloc] init]

这将有效:

UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mybutton setImage:[UIImage imageNamed:@"myImage"] forState:UIControlStateNormal];
mybutton.tintColor = [ODTheme blueTintColor];

答案 4 :(得分:4)

今天,我也遇到了这个问题。我用委托来解决它。

[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");
}

答案 5 :(得分:3)

应该尝试这种方法:

- (void)setTitleColor:(UIColor *)color
             forState:(UIControlState)state

答案 6 :(得分:3)

我发现必须遵循3件事。

<强> 1。将图片渲染为Default

转到Images.xcassets > Your Image > Show the Attributes inspector > Render As > Default

render as default

<强> 2。确保您的按钮类型为系统

第3。现在更改按钮的tintColor。

完成。

答案 7 :(得分:2)

只需将您的UIButton设置为在 Storyboard 中输入系统

enter image description here

然后在代码中使用:

myButton.tintColor = UIColor.whiteColor()

答案 8 :(得分:2)

在iOS 13中,您可以为图像着色,然后将其设置为按钮:

<groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.1.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.1</version>
</dependency>

<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>3.1.0</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.3</version>
</dependency>

答案 9 :(得分:1)

要更改可以使用的按钮文本的颜色:

Flow Layout

或OBJ-C:

resetButton.setTitleColor(UIColor.blackColor(), forState: .Normal)

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instm/UIButton/setTitleColor:forState

答案 10 :(得分:1)

如果您的UIButton类型是系统,那么只有色彩属性才有效。因此,首先需要将按钮类型设置为系统,然后为特定状态应用色调颜色。

let btn = UIButton.init(type: .system)
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

答案 11 :(得分:0)

只需使用:

[yourButton setBackgroundImage:[yourButton.currentBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; 
[yourButton setTintColor:[UIColor blackColor]];

答案 12 :(得分:0)

雨燕4:

yourButton.setTitleColor(UIColor.white,for:.normal)