为什么不UIButton.imageView setTintColor:在IBAction中工作

时间:2014-03-31 02:01:56

标签: ios objective-c uiimageview uibutton

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

这有效

- (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;
    }


}

1 个答案:

答案 0 :(得分:0)

根据iOS Developer Library

  

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

此处定义tintColorDidChange方法:

UIView tintColorDidChange

  

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

     

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