我知道以前有人问过类似的问题,但大多数答案都说创建一个具有所需颜色的新详细信息披露按钮图像(例如 How to change color of detail disclosure button for table cell)。
我希望能够在运行时动态地将它更改为我选择的任何颜色,因此使用预配置图像是不可行的。
通过阅读,我认为可能有几种方法可以做到这一点,但我不确定哪种方法最好,或者具体如何操作:
在代码中绘制所需的色圈,并在圆圈外和箭头右箭头处叠加阴影图像(其余部分有清晰的alpha channel ,因此绘制的色圈仍然可见)
在 UIImageView 中添加圆外圆的阴影图像,并作为 mask ,在阴影圆内填充,然后覆盖箭头。
添加灰度图像,用自身遮盖它,并覆盖所需的颜色(例如 http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage ),然后用箭头图像覆盖它。
什么是最好的方法,有没有人有任何代码可以准确说明如何做到这一点?
最佳答案
我使用以下辅助方法对灰度图像进行着色。它采用灰度图像、色调颜色和用于确保色调仅发生在部分灰度图像中的选项蒙版图像。此方法还确保新图像具有与原始图像相同(如果有)的可调整大小的插图。
+ (UIImage *)tintImage:(UIImage *)baseImage withColor:(UIColor *)color mask:(UIImage *)maskImage {
UIGraphicsBeginImageContextWithOptions(baseImage.size, NO, baseImage.scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSaveGState(ctx);
if (maskImage) {
CGContextClipToMask(ctx, area, maskImage.CGImage);
} else {
CGContextClipToMask(ctx, area, baseImage.CGImage);
}
[color set];
CGContextFillRect(ctx, area);
CGContextRestoreGState(ctx);
CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
CGContextDrawImage(ctx, area, baseImage.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (!UIEdgeInsetsEqualToEdgeInsets(baseImage.capInsets, UIEdgeInsetsZero)) {
newImage = [newImage resizableImageWithCapInsets:baseImage.capInsets];
}
return newImage;
}
这是非视网膜灰度细节披露图像:
这是视网膜版本:
这是非视网膜 mask (在引号之间 - 它大部分是白色的): " "
和视网膜面具(引号之间: " "
关于ios - 在代码中动态更改详细信息披露按钮的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16696697/