更改buttonImage的UIColor

时间:2015-11-17 10:17:18

标签: ios objective-c uibutton uiimage uicolor

我有UIImage redColor,但我想在whiteColor中展示它,怎么办?这是我的代码:

UIImage *buttonImage = [UIImage imageNamed:@"close-button.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0,
                           0.0, 
                           buttonImage.size.width,
                           buttonImage.size.height);

UIBarButtonItem *aBarButtonItem = 
  [[UIBarButtonItem alloc] initWithCustomView:aButton];

[aButton addTarget:self action:@selector(didSelectBackButton:) 
               forControlEvents:UIControlEventTouchUpInside];

[self.navigationItem setLeftBarButtonItem:aBarButtonItem];

3 个答案:

答案 0 :(得分:0)

如果您要更改UIColor的{​​{1}},请在UIImage interface内使用此方法:

UIImage

答案 1 :(得分:0)

试试这个:

UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
UIImage *img = [UIImage imageNamed:imageName];
icon.image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
icon.tintColor = tintColor;
icon.contentMode = UIViewContentModeScaleAspectFill;

答案 2 :(得分:0)

只需使用renderingMode添加图像,然后再设置色调。 此外,您不希望使用色调颜色渲染的背景和图像的任何部分必须完全透明。

https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming

UIImage *buttonImage = [[UIImage imageNamed:@"close-button.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

[aButton setTintColor:[UIColor whiteColor]];