我想要做的就是呈現酥料餅與annotation作爲其源視圖/錨如何設置MGLAnnotationView作爲sourceView爲酥料餅
使用委派功能didSelect annotationView我應該能夠做到這一點,但它似乎並沒有運行。 現在我只是通過didSelect註釋提出我酥料餅,並設置sourceView到導航欄,只是爲了顯示它的地方......
FYI: 我已經實現了Mapbox SDK到項目。 我沒有問題使用MapKit執行相同的任務。
有沒有人有什麼想法我可以做到這一點?
下面的代碼片段:
import UIKit
import Mapbox
class ViewController: UIViewController, MGLMapViewDelegate, UIPopoverPresentationControllerDelegate {
@IBOutlet var theMap: MGLMapView!
override func viewDidLoad() {
super.viewDidLoad()
theMap.delegate = self
let point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: 55.6, longitude: 13.0)
point.title = "Some place"
point.subtitle = "Malmö, Sweden"
theMap.addAnnotation(point)
}
func mapView(_ mapView: MGLMapView, didSelect annotationView: MGLAnnotationView) {
print("annotation view: ", annotationView)
// this method doesn't seem to get called at all...
// but ideally this is the place to present the popover.
}
func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {
print("annotation: ", annotation)
// present the popover
presentPopover()
}
func presentPopover(){
let popover = storyboard?.instantiateViewController(withIdentifier: "MyCalloutVC") as! MyCallout
popover.modalPresentationStyle = UIModalPresentationStyle.popover
popover.popoverPresentationController?.backgroundColor = UIColor.white
popover.popoverPresentationController?.delegate = self
// I would like to set the source anchor to the selected annotation view.
popover.popoverPresentationController?.sourceView = UINavigationBar() // set to nav bar for now...
popover.popoverPresentationController?.permittedArrowDirections = .any
// popover size set in MyCallout
self.present(popover, animated: true)
}
}
謝謝@ ppoh71爲您的解釋性答案,適當的東西!我現在很好。 –