UIButton默认标题颜色iOS7

Roh*_*mer 7 uibutton ios

我想知道如何将UIButton的标题颜色更改回默认值.

我有一个按钮,我更改了标题颜色,表示某个东西已打开,就像这样;

[cell.offStateSwitch setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

然后当它关闭时,我想将其更改回默认颜色.

我无法弄清楚如何获得UIButton的默认系统颜色.我发现许多人通过强制使用特定的RGB值来做到这一点,但在不同版本的iOS中这不一定正确.

小智 11

将颜色设置为nil,它的行为将返回到更改之前的状态,包括正确响应警报.

[myButton setTitleColor:nil forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)


fis*_*ear 9

我假设你的意思是你要将文本设置回tintColor.如果你只想要当前的tintColor,你可以这样做:

[button setTitleColor:button.tintColor forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

但是,tintColor应该在某些情况下自动更改,例如弹出警报时.为了实现这一点,我认为您需要定义自己的按钮:

@interface MyButton : UIButton
@end

@implementation MyButton

- (void) tintColorDidChange
{
    [self setTitleColor:self.tintColor forState:UIControlStateNormal];
    [super tintColorDidChange];
}

@end
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我建议您使用所选状态为您特殊颜色:

[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)

button.selected = YES正如你所说,当"某事正在进行"时设定.


use*_*734 5

button.tintColor = nil
Run Code Online (Sandbox Code Playgroud)

要么

[button setTintColor: null]
Run Code Online (Sandbox Code Playgroud)

在iOS v7.0中,UIView的所有子类都从基类派生tintColor的行为.有关更多信息,请参阅UIView级别的tintColor讨论.

对于类型为UIButtonTypeCustom的按钮,此属性没有默认效果.对于自定义按钮,您必须自己实现与tintColor相关的任何行为.

将UIButton的tintColor设置为nil(null)将重置其标题的颜色


nul*_*ull 1

iOS 中没有默认标题颜色的属性,您可以简单地获取它。

只需定义默认颜色:

UIColor* defaultColor; 然后在你的viewDidLoad放置或视图初始化的任何地方:

defaultColor = [button titleColorForState: UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

然后您将defaultColor作为默认标题颜色。