2014-03-31 24 views
1

我试图改变UIButton图像的颜色。在viewDidLoad方法中,我将着色颜色更改为“appColor”或灰色,工作正常。当用户点击按钮时,我尝试再次更改色调颜色,但没有任何反应。我甚至试图用UIButton.imageView setImage来改变图像,也没有任何反应。我究竟做错了什么?为什么不UIButton.imageView setTintColor:工作在IBAction

这工作

- (void)viewDidLoad{ 
    if ([checkActivityArray containsObject:[place objectId]]){ 
     [checkCount setTextColor:appColor]; 
     checkButton.imageView.image = [checkButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
     [checkButton.imageView setTintColor:appColor]; 
    } 
    else{ 
     [checkCount setTextColor:[UIColor colorWithRed:170.0/255.0 green:170.0/255.0 blue:170.0/255.0 alpha:1]]; 
     checkButton.imageView.image = [checkButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
     [checkButton.imageView setTintColor:[UIColor colorWithRed:170.0/255.0 green:170.0/255.0 blue:170.0/255.0 alpha:1]]; 
    } 
} 

这不

- (IBAction)checkMarkButton:(UIButton *)sender { 
    sender.enabled = NO; 
    CheckMarkController *checkMark = [[CheckMarkController alloc]init]; 
    BOOL didComplete = NO; 

    if ([checkActivityArray containsObject:[place objectId]]){ 
     didComplete = [checkMark removeCheckMark:place]; 
    }else{ 
     didComplete = [checkMark addCheckMark:place]; 
    } 
    if (didComplete) { 

     checkActivityArray = [[NSMutableArray alloc] initWithContentsOfFile:checkMarkArrayFileName]; 

     int tempInt = [checkCount.text intValue]; 

     if ([checkActivityArray containsObject:[place objectId]]){ 
      [sender.imageView setImage:[checkButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]]; 
      [sender.imageView setTintColor:appColor]; 
      [checkCount setTextColor:appColor]; 
      tempInt++; 
      checkCount.text = [NSString stringWithFormat:@"%d", tempInt]; 
     } 
     else{ 
      sender.imageView.image = [sender.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
      [sender.imageView setTintColor:[UIColor colorWithRed:170.0/255.0 green:170.0/255.0 blue:170.0/255.0 alpha:1]]; 
      [checkCount setTextColor:[UIColor colorWithRed:170.0/255.0 green:170.0/255.0 blue:170.0/255.0 alpha:1]]; 
      tempInt--; 
      checkCount.text = [NSString stringWithFormat:@"%d", tempInt]; 
     } 
     sender.enabled = YES; 
    } 


} 

回答

0

按照iOS Developer Library

要刷新子视图渲染时此属性更改,覆盖tintColorDidChange方法。

而且tintColorDidChange方法定义如下:

UIView tintColorDidChange

系统时,你的代码更改该视图的tintColor属性的值调用视图此方法。另外,系统在继承改变的交互色调颜色的子视图上调用该方法。

在您的实施中,根据需要刷新视图渲染。

+0

我需要在tintColorDidChange中放置什么? – david2391

+0

我试过[self setNeedsDisplay];仍然没有什么 – david2391

+0

调用tintColorDidChange方法吗? – user1459524