2014-12-03 85 views
1

我已經研究過這個問題,很遺憾,我還沒有得到任何針對該問題的解決方案。自定義MKAnnotationView標註不顯示

的問題是這樣的:

我有一個的drawRect定製MKAnnotationView覆蓋。我無法使用圖像屬性,因爲我有一個名爲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..."

+1

如果你可以指出在代碼中犯的一些錯誤,我會非常感激。 – Blip 2014-12-03 22:10:55

+0

'canShowCallout'是否設置爲'true'?在'opaque = false'之後,嘗試'canShowCallout = true'。 – Anna 2014-12-04 12:02:43

+0

@Anna謝謝你的回答,因爲它的工作!你讓我今天一整天都感覺很好。 – Blip 2014-12-04 19:25:23

回答

-1

第一件事情是colorDot = color所以,如果你想讓你在mapview的viewForAnnotation函數中做標註顯示。

以下爲示例代碼,您可以檢查:

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

我不明白你想究竟改變什麼?標註背景顏色或文字或其他內容?

中標註更改元素的正確方法是addSubview(無論是通過程序創建的UIView或使用廈門國際銀行)

如果您在標註有許多人跡罕至的東西,添加的東西的最佳方式是不帶邊框。嘗試添加圖層或UIImage與你的東西