【问题标题】:iOS 8 MKAnnotationView rightCalloutAccessoryView misalignediOS 8 MKAnnotationView rightCalloutAccessoryView 未对齐
【发布时间】:2014-10-18 12:38:29
【问题描述】:

总的来说,我对 iOS 的东西还是很陌生,在测试我们的应用程序的 iOS 8 兼容性时发现了一个问题。 在 iOS 7 中一切正常,但在 iOS 8 中,rightCalloutAccessoryView 在某些情况下会错位。

第一张截图:正确对齐

第二张截图:错误对齐

如您所见,当信息窗口的标题很长时,就会出现问题。但在 iOS 7 上情况并非如此,我找不到任何提及这已更改的内容?

我试图了解原因并找到解决此问题的方法,但还没有找到任何东西。 这是否可以由我们自己解决,还是我们必须向 Apple 提出问题?

任何帮助/想法都将受到高度赞赏,因为我对这个问题感到不知所措。

添加 MKAnnotationViews 的代码

- (MKAnnotationView *)viewForStation:(id<MKAnnotation>)annotation {
      static NSString *stationIdentifier = @"stao";
      MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:stationIdentifier];
      if (annotationView == nil) {
          annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation           reuseIdentifier:stationIdentifier];
          annotationView.image = [UIImage bundleImageNamed:PIN_IMAGE];
          annotationView.canShowCallout = YES;
          annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
      }
      return annotationView;
}

您可能感兴趣,我(刚刚)也将此问题添加到 apple.developer.com 上的 iOS 8 Beta 版块:https://devforums.apple.com/thread/242282?tstart=0

如果我在这里或 apple.developer.com 上得到答案,我会反映它,以便我们可以在两个网站上找到它

【问题讨论】:

  • 还没有人有想法吗?
  • 我也有同样的问题 :(
  • 我自己也是,我无法解决这个问题:(
  • 同样的问题...
  • 他们将我的问题作为副本关闭,没有任何评论,您甚至无法查看待处理的问题。福苹果!

标签: ios mapkit ios8 mkannotationview


【解决方案1】:

我通过设置 autoresizingmask 来修复垂直错误和 frame.size.width 来解决水平错误

UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton setFrame:CGRectMake(0, 0, CGRectGetWidth(infoButton.frame)+10, CGRectGetHeight(infoButton.frame))];
[infoButton setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin];
[annotationView setRightCalloutAccessoryView:infoButton];

【讨论】:

    【解决方案2】:

    真丑,但我确实设法找到了解决这个问题的方法。

    将附件视图高度设置为等于标注视图高度的值。我使用了硬编码值:

    • 对于 iOS 7 目标:45.0 像素
    • 对于 iOS 8 目标:54.0 像素

    (您可能注意到默认注释标注视图的高度在 iOS 8 中比在 iOS 7 中更大)。

    在您的代码中,这将是:

    - (MKAnnotationView *)viewForStation:(id<MKAnnotation>)annotation {
      static NSString *stationIdentifier = @"stao";
      MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:stationIdentifier];
      if (annotationView == nil) {
          annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation 
                                                        reuseIdentifier:stationIdentifier];
          annotationView.image = [UIImage bundleImageNamed:PIN_IMAGE];
          annotationView.canShowCallout = YES;
    
          UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    
          // N.B. In production code, you would need a more generic way to adjust 
          // height values instead of hard-coding values based on NSFoundationVersionNumber...
    
          CGFloat height = (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) ? 55.f : 45.f;
    
          detailButton.frame = CGRectMake(0.f, 0.f, 32.f, height);
          annotationView.rightCalloutAccessoryView = detailButton;
      }
      return annotationView;
    }
    

    此外,就我目前所见,如果您同时指定左 右附件视图,则必须将 两个 的高度设置为相同的值得到正确的对齐。希望这个问题会在后续的 iOS 版本中得到修复,以避免编写这种代码......

    【讨论】:

    • 至少有一些解决方案。但我不确定我们是否应该等待苹果公司的修复。似乎他们的软件越来越糟糕:(
    • 我发现我也必须调整宽度,否则按钮很难靠在标注的右边缘。我也只在版本为 8 或以上的情况下调整框架;如果版本小于 8,我会离开现有框架。
    【解决方案3】:

    您好,我遇到了完全相同的问题,这是我为解决它所做的:

    这似乎是 Callout 视图在显示 MKAnnotation 标题文本过长时出现问题的问题。即使从您的屏幕截图中,您也可以看到该问题仅在标题被截断并添加省略号时才会发生。所以标准的 MKAnnotation 没有正确布局视图,这是 iOS8 中的一个错误。

    同时,您可以按照here 的步骤创建自己的自定义标注。

    我认为这对我需要的东西来说太多了,所以我只是修剪了标题字符串并添加了省略号以防止它尝试自己做。 (不理想,但这是一个快速的解决方案,而且文本会被截断)

    if (locTitle.length > 15) 
    {
        locTitle = [NSString stringWithFormat:@"%@...", [locTitle substringToIndex:14]];
    }
    

    【讨论】:

      【解决方案4】:

      我注意到的一个好奇心是,我要显示的第一个标注会正确对齐附件视图,但随后的选择不会。在返回出列注释视图之前,我添加了对 [view setNeedsLayout] 的调用,一切都开始重新正确对齐。

      如果不花更多时间在这上面,我最好的猜测是不会为由于视图层次结构没有改变而出队的视图触发重新布局。

      不过,我强烈建议为此问题提交一份雷达。不必为此类标准用例实施变通方法。

      编辑:

      虽然在许多情况下这似乎仍然更好,但我仍然遇到这个问题,因为注释的标题长到可以被截断。

      【讨论】:

        【解决方案5】:

        现在,我用

        隐藏了 rightCalloutAccessoryView
        pin.rightCalloutAccessoryView.frame = CGRectZero;
        

        然后单击 Annotation View 仍然有效,只是右侧没有按钮。 在 iOS7 和 iOS8 上测试,不确定 iOS6。

        【讨论】:

          【解决方案6】:

          我通过在添加注释之前设置我的地图视图的委托来让我的工作,并且不必更改 del 中的任何内容。

          从以下开始:

              self.mapView = [[MKMapView alloc] init];
              NGAAnnotation *annotation = [[NGAAnnotation alloc] init];
              annotation.coordinate = coordinates;
              annotation.title = self.project.name;
          
              [self.mapView addAnnotation:annotation];
              self.mapView.delegate = self;
          

          收件人:

              self.mapView = [[MKMapView alloc] init];
              NGAAnnotation *annotation = [[NGAAnnotation alloc] init];
              annotation.coordinate = coordinates;
              annotation.title = self.project.name;
          
              self.mapView.delegate = self;
              [self.mapView addAnnotation:annotation];
          

          为我修好了。我注意到添加注释时会调用委托方法,因此如果在添加注释之前未设置委托,它将不会被调用。

          【讨论】:

          • 这对我不起作用。我已经通过情节提要设置了委托,但即使在添加注释之前明确设置它也不能解决问题。
          • 您的委托是否定义了注释方法的视图?
          【解决方案7】:

          在 Swift 3 中:

          let infoButton = UIButton(type: .detailDisclosure)
          annotationView.rightCalloutAccessoryView = infoButton
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-09-12
            • 1970-01-01
            • 2020-07-01
            • 2015-01-20
            相关资源
            最近更新 更多