2017-02-22 38 views
0

我想調整一個UIImage的大小,以適應leftCalloutAccessoryView插槽中的地圖視圖的註釋。我試圖(很差)添加約束,但圖像沒有顯示 - 沒有它顯示的限制,但它太大了。現在和他們在一起,它並沒有出現,我在想,因爲它已經被改變了視野。我嘗試添加一個leftAnchortopAnchor,但出現錯誤,因爲我不完全確定我應該將其固定在哪個附件視圖上?註釋視圖?反正這裏的代碼迄今:調整大小的圖像,以適應MapView的leftCalloutAccessoryView

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    let identifier = "Restaurants" 

    if annotation is Restaurants { 
     var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 

     if annotationView == nil { 
      annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      annotationView!.canShowCallout = true 

      let infoButton = UIButton(type: .detailDisclosure) 
      let imgView = UIImageView(image: UIImage(named: "TTT")) 

      // constraints 
      imgView.translatesAutoresizingMaskIntoConstraints = false 
      imgView.widthAnchor.constraint(equalToConstant: 20).isActive = true 
      imgView.heightAnchor.constraint(equalToConstant: 20).isActive = true 

      annotationView!.rightCalloutAccessoryView = infoButton 
      annotationView!.leftCalloutAccessoryView = imgView 
     } else { 
      annotationView!.annotation = annotation 
     } 
     return annotationView 
    } 
    return nil 
} 

難道我只是需要增加側面和頂部錨,或者是那裏要對這個完全不同的方式?

謝謝!

回答

1

你可以嘗試設置UIImageView大小創建MKPinAnnotationView

,然後調用方面適合於這樣的:

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: annotationView!.frame.height, height: annotationView!.frame.height)) 

imageView.image = UIImage(named: "TTT") 
imageView.contentMode = .scaleAspectFit 

annotationView!.leftCalloutAccessoryView = imageView 
+0

,像變魔術一樣,謝謝! – KingTim

相關問題