1
votes

I have researched this problem, unfortunately I have not gotten any solutions specific to the problem.

The problem is thus:

I have a custom MKAnnotationView with drawRect overridden. I cannot use the image property because I have this property called dotColor that impacts what the annotation view draws.

Code:

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)
        }
    }

}

But when I click the annotation view on the map, it doesn't show the callout (I am pretty sure the MKAnnotation has a title. You could check out this thread's answer: "If the title is nil, the callout...")

1
If you could point out some mistakes made in code, I would really appreciate it.Blip
Is canShowCallout set to true? After opaque = false, try doing canShowCallout = true.user467105
@Anna Thank you for your answer, because it worked! You made my day.Blip

1 Answers

-1
votes

The first thing is colorDot = color so, if you want make you callout show in mapview's viewForAnnotation function.

following is example code, you can check:

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

I doesn't understand what you want to change exactly? The callout background color or text or something else?

The correct way to change element in callout is addSubview (either creating UIView programatically or using xib)

If you have many inaccessible things in callout, the best way to add something is not with frame. Try add layer first or UIImage with your stuff