將 UIColor 應用於 UIImage
通過將 UIColor 應用於 UIImage 例項,使用與多個主題庫應用程式相同的 UIImage,如下所示。
// *** Create an UIImage instance with RenderingMode AlwaysTemplate ***
UIImage *imgMenu = [[UIImage imageNamed:@"iconMenu"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
// *** Now Apply `tintColor` to `UIImageView` of UIImageView or UIButton and convert image in given color ***
[btn setImage:imgMenu forState:UIControlStateNormal]; // Set UIImage in UIButton.
[button.imageView setTintColor:[UIColor blueColor]]; // It changes image color of UIButton to blue color
現在假設你想要使用 UIImageView 執行相同操作,然後使用以下程式碼
[imageView setImage:imgMenu]; // Assign UIImage to UIImageView
[imageView setTintColor:[UIColor greenColor]]; // Change imageview image color to green.
[imageView setTintColor:[UIColor redColor]]; // Change imageview image color to red.