【问题标题】:Callout for custom MKAnnotationView not showing未显示自定义 MKAnnotationView 的标注
【发布时间】:2014-12-03 22:09:27
【问题描述】:

我已经研究过这个问题,很遗憾我没有得到任何针对这个问题的解决方案。

问题是这样的:

我有一个覆盖了 drawRect 的自定义 MKAnnotationView。我不能使用 image 属性,因为我有一个名为 dotColor 的属性,它会影响注释视图的绘制。

代码:

class CustomAnnotationView: MKAnnotationView {

    var dotColor: UIColor!

    init(annotation: CustomAnnotation, color: UIColor) {
        super.init(annotation: annotation, reuseIdentifier: "CustomAnnotationView")

        self.annotation = annotation
        frame.size = CGSize(width: 20, height: 20)
        dotColor = color
        opaque = false
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func drawRect(rect: CGRect) {
        if annotation != nil && dotColor != nil {
            let context = UIGraphicsGetCurrentContext()

            let circleFrame = CGRectInset(rect, 2, 2)//Needs the inset to show the stroke completely

            CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
            CGContextSetStrokeColorWithColor(context, dotColor.CGColor)
            CGContextSetLineWidth(context, 3)

            CGContextFillEllipseInRect(context, circleFrame)
            CGContextStrokeEllipseInRect(context, circleFrame)
        }
    }

}

但是当我点击地图上的注释视图时,它没有显示标注(我很确定 MKAnnotation 有一个标题。你可以查看这个线程的答案:"If the title is nil, the callout..."

【问题讨论】:

  • 如果您能指出代码中的一些错误,我将不胜感激。
  • canShowCallout 是否设置为 true?在opaque = false 之后,尝试做canShowCallout = true
  • @Anna 谢谢你的回答,因为它有效!你让我开心。

标签: ios objective-c swift map


【解决方案1】:

第一件事是colorDot = color,所以,如果你想让你的标注显示在mapview的viewForAnnotation函数中。

以下是示例代码,您可以查看:

v = MKAnnotationView(annotation: annotation, reuseIdentifier: "here put your identifier") v!.canShowCallout = true

我不明白你到底想改变什么?标注背景颜色或文本或其他内容?

在标注中更改元素的正确方法是addSubview(以编程方式创建 UIView 或使用 xib)

如果标注中有很多无法访问的内容,添加内容的最佳方法不是使用框架。尝试先添加层或UIImage 与您的东西

【讨论】:

    猜你喜欢
    • 2011-12-22
    • 2010-11-23
    • 2015-07-19
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多