【问题标题】:UIButtonTypeDetailDisclosure Color ChangeUIButtonTypeDetailDisclosure 颜色变化
【发布时间】:2013-11-26 06:19:56
【问题描述】:

我已将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;

【问题讨论】:

  • carcar.png的宽高是多少?
  • @AnnaKarenina 它是 100x100
  • 尝试将其缩小到 32 x 32。
  • @AnnaKarenina 不幸的是它没有显示出来。
  • 您是否使用 UIButtonTypeCustom 并且您是否将 infoButton.frame 设置为 0、0、32、32?

标签: ios uibutton ios7 uiimage mkannotationview


【解决方案1】:

使用自定义按钮类型:

infoButton = [UIButton buttonWithType:UIButtonTypeCustom];

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

【讨论】:

  • 谢谢,但我什么都没有,它是空的。
【解决方案2】:

看起来 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];

【讨论】:

  • 天哪。如果您确实添加了 Antonio 的建议会怎样?
  • 对不起,你的意思是什么?
  • 如果使用[UIButton buttonWithType:UIButtonTypeCustom]设置图片的渲染模式会怎样?
  • 我使用了@AntonioMG 答案[UIButton buttonWithType:UIButtonTypeCustom],但它仍然没有显示任何内容。
  • 您是否同时尝试了这两种方法?使用UIButtonTypeCustom设置图像的渲染模式?
【解决方案3】:

我遇到了类似的问题并解决如下(使用您的代码):

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

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

[infoButton setBackgroundImage:carImage forState:UIControlStateNormal];

【讨论】:

    【解决方案4】:

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

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

    infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    // set image
    [infoButton setImage:yourImage forState:UIControlStateNormal];
    
    // More elegant then setting hardcoded frame
    [infoButton sizeToFit];
    

    【讨论】:

      【解决方案5】:

      使用这个

      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;
      

      【讨论】:

        猜你喜欢
        • 2011-04-12
        • 2013-03-30
        • 1970-01-01
        • 2013-08-15
        • 1970-01-01
        • 2015-01-19
        • 2016-08-14
        • 2015-07-30
        • 1970-01-01
        相关资源
        最近更新 更多