我已經研究過這個問題,很遺憾,我還沒有得到任何針對該問題的解決方案。自定義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...")
如果你可以指出在代碼中犯的一些錯誤,我會非常感激。 – Blip 2014-12-03 22:10:55
'canShowCallout'是否設置爲'true'?在'opaque = false'之後,嘗試'canShowCallout = true'。 – Anna 2014-12-04 12:02:43
@Anna謝謝你的回答,因爲它的工作!你讓我今天一整天都感覺很好。 – Blip 2014-12-04 19:25:23