2015-09-15 15 views
14

WWDC video 206を見てから、これは細部コールアウトビューをmapViewアノテーションビューに追加するのは簡単な作業だと思っていました。MapKit iOS 9 detailCalloutAccessoryView usage

私は何か間違っていると思います。私はこれだけ

enter image description here

を取得

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 

    let view:MKAnnotationView! 

    if let dequed = routeMapView.dequeueReusableAnnotationViewWithIdentifier("pin") { 
     view = dequed 
    } 
    else { 
     view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin") 
    } 

    let x = UIView(frame: CGRectMake(0, 0, 200, 200)) 
    x.backgroundColor = UIColor.redColor() 

    // shows the red 
    //view.leftCalloutAccessoryView = x 

    // working as no subtitle - but no red view 
    view.detailCalloutAccessoryView = x 

    view.canShowCallout = true 
    return view 
} 

を設定し、私のピンビューで

私はleftCalloutAccessoryViewでそれをしようとした場合、私は enter image description here

取得するので、私は、ビューが機能している知っています

私は何かが欠けているはずです。私はちょうど

view.detailCalloutAccessoryView = UIImage(named:"YourImageName") 

画像のようdetailCalloutAccessoryViewに画像を追加するとサイズが正しくなど

私はちょうど私自身のカスタムビューに配置する方法を見つけ出すことはできません、があり、注意してください。

おかげ

答えて

14

あなたの視野の幅と高さのためにいくつかの制約を追加する必要があります。intrinsicContentSizeも作品を追加

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    var av = mapView.dequeueReusableAnnotationViewWithIdentifier("id") 
    if av == nil { 
     av = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "id") 
    } 

    let myView = UIView() 
    myView.backgroundColor = .greenColor() 

    let widthConstraint = NSLayoutConstraint(item: myView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40) 
    myView.addConstraint(widthConstraint) 

    let heightConstraint = NSLayoutConstraint(item: myView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 20) 
    myView.addConstraint(heightConstraint) 

    av!.detailCalloutAccessoryView = myView 
    av!.canShowCallout = true 

    return av! 
} 

Munich 1972

いくつかのテストを行ったところ、detailCalloutAccessoryViewに設定すると、MapKitによって自動的にtranslatesAutoresizingMaskIntoConstraintsがfalseに設定されることがわかりました。

WWDC 2015セッション"What's New in MapKit"では、「自動レイアウトはサポートされている」と言われました。私はAppleが本当に意味することは、あなたが自動レイアウトを使用する必要があると思う?

+0

ええ、それはいい仕事です。 – DogCoffee

+3

'detailCalloutAccessoryView'の周りに追加された間隔をどのように取り除きますか? @ Klaas – ScottOBot

+0

@ScottOBotはそれを試みたことはありません。しかし、.Widthと.Heightの代わりに.Top、.Left、.Bottom、および.Rightの制約を使用することで可能です。 – Klaas

0

使用UIImageView

view.detailCalloutAccessoryView = UIImageView(image:UIImage(named:"YourImageName")) 
+0

画像と画像のビューで作業することは問題ありません。私はカスタムビューを使いたいです。 – DogCoffee

8

1.のUIViewを作成し、あなたにそれを追加するには、サイズを設定することができます。ここ

enter image description here

ストーリーボードにVCをマップ、制約、ボタン、イメージなどの追加 - あなたが望むレイアウト。スタックビューはこの場合完全に機能します。

2.作成し、アウトレットカスタムビューから、通常通りご地図VC

コントロールをドラッグします。

@IBOutlet var customDetailView: UIView! 

あなたのピンのdetailCalloutAccessoryViewを設定します。3.

例えば

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 
    view.detailCalloutAccessoryView = customDetailView 
} 

成功

enter image description here

+2

これは明らかなように思えるかもしれませんが、それは数分間私を捕まえました...ストーリーボードのオープンスペースにUIViewをドラッグしようとすると動作しません。あなたはそれを使用したいUIViewControllerの上の一番上のバーにドラッグする必要があります。その黄色の円の隣に、赤いボックスなど... –

+1

@ByronCoetsee、それはおそらく答えほど素晴らしいです。アップルがなぜこの種のイースターエッグの機能を果たしたのだろうか? – Eduardo

+0

驚くばかり!私はそれをどのようにパーソナライズすることができるのだろうかと思っていました...ボタンを置いて、いくつかのアクションやカスタムテキストをダイナミックテキストで作ってみましょう。ありがとう! – Cleversou