UIButton默认标题颜色iOS7

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

我有一个button,我改变标题的颜色来表示一个东西是在这样的;

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

然后,当它closures时,我想将其更改回默认颜色。

我不知道如何获得UIButton的默认系统颜色。 我发现很多人通过强制特定的RGB值来实现这一点,但是在不同版本的iOS中这不一定是正确的。

设置颜色为零,它的行为将返回到它之前,你已经改变它,包括正确的响应警报。

 [myButton setTitleColor:nil forState:UIControlStateNormal]; 

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

 [button setTitleColor:button.tintColor forState:UIControlStateNormal]; 

但是,tintColor在某些情况下应该会自动改变,比如popup警告。 为了实现这一点,我认为你需要定义你自己的button:

 @interface MyButton : UIButton @end @implementation MyButton - (void) tintColorDidChange { [self setTitleColor:self.tintColor forState:UIControlStateNormal]; [super tintColorDidChange]; } @end 

在这种情况下,我build议你使用选定的状态为你特殊的颜色:

 [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; 

并设置button.selected = YES当“东西是在”,如你所说。

 button.tintColor = nil 

要么

 [button setTintColor: null] 

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

对于types为UIButtonTypeCustom的button,此属性没有默认效果。 对于自定义button,您必须自己实现与tintColor相关的任何行为。

设置UIButton的tintColor为零(空)将重置其标题的颜色

在iOS中没有默认标题颜色的属性,你可以简单地得到它。

只需定义defaultColor:

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

 defaultColor = [button titleColorForState: UIControlStateNormal]; 

然后你有defaultColor作为默认的标题颜色。

在cellForRowAtIndexPath委托中添加下面的代码……

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath cell.button.tag=indexPath.row+100; [cell.button addTarget:self selector:@selector(changeButtonTitle:) forControlEvents:UIcontrolTouchUpEventInside]; 

然后在button操作中添加下面的代码

 -(void)changeButtonTitle:(UIButton *)button { UITableViewCell *cell=(UITableViewCell *)button.superview.superview; NSIndexpath *indexpath=[self.yourtableview indexPathForCell:cell]; UIButton *tempButton=(UIButton *)[cell viewWithtag:indexPath.row+100]; [tempButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; }