4

我已将图像更改为下图,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;
4

5 回答 5

3

我有一个类似的问题并解决如下(使用您的代码):

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton setTintColor:[UIColor clearColor]];

UIImage *carImage = [[UIImage imageNamed:@"carcar.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

[infoButton setBackgroundImage:carImage forState:UIControlStateNormal];
于 2013-11-19T11:34:14.480 回答
2

使用自定义按钮类型:

infoButton = [UIButton buttonWithType:UIButtonTypeCustom];

如果要添加自己的图像,则应始终使用此方法。其他类型用于预定义按钮(如信息、添加联系人等)

于 2013-11-13T15:14:27.667 回答
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];
于 2013-11-13T16:53:46.583 回答
0

您需要使用不同的按钮类型并设置它的框架。

您什么也看不到,因为您的按钮框架为零(对于 UIButtonTypeCustom)。因此,为了解决您的问题,您可以:

infoButton = [UIButton buttonWithType:UIButtonTypeCustom];

// set image
[infoButton setImage:yourImage forState:UIControlStateNormal];

// More elegant then setting hardcoded frame
[infoButton sizeToFit];
于 2014-02-06T10:07:39.343 回答
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;
于 2014-11-17T06:13:29.983 回答