set TintColor to MKAnnotationView image(将 TintColor 设置为 MKAnnotationView 图像)
问题描述
我正在为 iOS 7.0+
编写一个应用程序,我想使用新功能:imageWithRenderingMode
.我有一个带有此代码的地图注释:
I'm writing an application for iOS 7.0+
and I wanted to use new feature witch is: imageWithRenderingMode
. I have a map annotation with this code:
-(MKAnnotationView*)annotationView {
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"AnnotationIdentifier"];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [[UIImage imageNamed:@"star"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.tintColor = [UIColor redColor];
return annotationView;
}
我预计我的图钉会是红色的,但保持黑色,只有标注 (i) 图标变为红色.
I was expected that my pins will be red, but stays black, only the callout (i) icon turns red.
我试图在我的 ViewController
中设置 self.view.tintColor
和 self.mapView.tintColor
但这也不起作用.如何将这个引脚变成红色?
I was trying to set self.view.tintColor
and self.mapView.tintColor
in my ViewController
but this not working either. How to turns this pins red?
推荐答案
有一个比不涉及创建 UIImageView
的提议更好的解决方案.
There's a better solution than those proposed that doesn't involve creating a UIImageView
.
此 Swift 代码将创建您的 UIImage
的彩色版本.
This Swift code will create a colored version of your UIImage
.
extension UIImage {
func colorized(color : UIColor) -> UIImage {
let rect = CGRectMake(0, 0, self.size.width, self.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);
let context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, .Multiply)
CGContextDrawImage(context, rect, self.CGImage)
CGContextClipToMask(context, rect, self.CGImage)
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let colorizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return colorizedImage
}
}
这样称呼它:
UIImage(named: "myImage")!
.imageWithRenderingMode(.AlwaysTemplate)
.colorized(UIColor.red())
这篇关于将 TintColor 设置为 MKAnnotationView 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 TintColor 设置为 MKAnnotationView 图像


- android 4中的android RadioButton问题 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01