【问题标题】:Why doesn't UIButton.imageView setTintColor: work in IBAction为什么 UIButton.imageView setTintColor: 在 IBAction 中不起作用
【发布时间】:2014-03-31 02:01:55
【问题描述】:

我正在尝试更改 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;
    }


}

【问题讨论】:

    标签: ios objective-c uiimageview uibutton


    【解决方案1】:

    根据iOS Developer Library

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

    这里定义了tintColorDidChange方法:

    UIView tintColorDidChange

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

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

    【讨论】:

    • 我需要在 tintColorDidChange 中添加什么?
    • tintColorDidChange方法调用了吗?
    • 尝试在 [self setNeedsDisplay] 之前调用 [super tintColorDidChange]
    • 试试 [self.view setNeedsDisplay];
    • 在“UIButton”类型的对象上找不到属性“视图”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    相关资源
    最近更新 更多