我已将UIButtonTypeDetailDisclosure
的图像更改为下图,但问题是图像变为蓝色。我的问题在哪里?
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
*infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton setImage:[UIImage imageNamed:@"carcar.png"] forState: UIControlStateNormal];
pinView.rightCalloutAccessoryView = infoButton;
}
上次更新:使用上一个infoButton.frame解决。
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *carImage = [UIImage imageNamed:@"carcar.png"];
if ([carImage respondsToSelector:@selector(imageWithRenderingMode:)])
{
carImage = [carImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
[infoButton setImage:carImage forState: UIControlStateNormal];
infoButton.frame = CGRectMake(0, 0, 32, 32);
pinView.rightCalloutAccessoryView = infoButton;
答案 0 :(得分:3)
我遇到了类似的问题并按如下方式解决(使用您的代码):
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton setTintColor:[UIColor clearColor]];
UIImage *carImage = [[UIImage imageNamed:@"carcar.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[infoButton setBackgroundImage:carImage forState:UIControlStateNormal];
答案 1 :(得分:2)
使用自定义按钮类型:
infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
如果要添加自己的图像,则应始终使用此方法。其他类型用于预定义按钮(如信息,添加联系人等)
答案 2 :(得分:1)
看起来iOS 7将您的图像视为模板。尝试替换
[infoButton setImage:[UIImage imageNamed:@"carcar.png"] forState: UIControlStateNormal];
与
UIImage *carImage = [UIImage imageNamed:@"carcar.png"];
if ([carImage respondsToSelector:@selector(imageWithRenderingMode:)])
{
carImage = [carImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
[infoButton setImage:carImage forState: UIControlStateNormal];
答案 3 :(得分:0)
您需要使用不同的按钮类型并设置它的框架。
您什么也看不见,因为按钮的框架为零(对于UIButtonTypeCustom)。因此,为了解决您的问题,您可以:
infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
// set image
[infoButton setImage:yourImage forState:UIControlStateNormal];
// More elegant then setting hardcoded frame
[infoButton sizeToFit];
答案 4 :(得分:0)
使用此
static NSString * const identifier = @"MyCustomAnnotation";
MKAnnotationView* annotationView = [mapView1 dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView)
{
annotationView.annotation = annotation;
}
else
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier];
}
//left image view
UIImageView *imageIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"car.png"]];
annotationView.leftCalloutAccessoryView = imageIcon;
//right button with image
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton setTintColor:[UIColor clearColor]];
UIImage *carImage = [[UIImage imageNamed:@"Search.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[infoButton setBackgroundImage:carImage forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = infoButton;
annotationView.image = [UIImage imageNamed:@"pin_image.png"];
annotationView.canShowCallout = YES;
return annotationView;