MapKit не отображает пользовательское изображение вывода примечаний на iOS9
Мой код отлично работал с iOS 7 до 8. С обновлением вчера пользовательские изображения на моих контактах были заменены стандартным пин-кодом.
Любые предложения?
Мой код:
extension ViewController: MKMapViewDelegate {
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView! {
if annotation is MKUserLocation {
return nil
}
let reuseId = String(stringInterpolationSegment: annotation.coordinate.longitude)
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.image = getRightImage(annotation.title!!)
}
let button = UIButton(type: UIButtonType.DetailDisclosure)
pinView?.rightCalloutAccessoryView = button
return pinView
}
}
Функция для получения изображения возвращает UIImage
на основе имени:
func getRightImage (shopName:String)-> UIImage{
var correctImage = UIImage()
switch shopName
{
case "Kaisers":
correctImage = UIImage(named: "Kaisers.jpg")!
default:
correctImage = UIImage(named: "sopiconsmall.png")!
}
return correctImage
}
Нет такой карты:
![ios9 без изображений]()
Ответы
Ответ 1
Вместо создания MKPinAnnotationView
создайте обычный MKAnnotationView
.
Подкласс MKPinAnnotationView
имеет тенденцию игнорировать свойство изображения, поскольку он предназначен для отображения только стандартных красных, зеленых, фиолетовых контактов (через свойство pinColor).
Когда вы переключаетесь на MKAnnotationView
, вам также нужно будет прокомментировать строку animatesDrop, так как это свойство относится к MKPinAnnotationView
.
Ответ 2
Следующий код отлично работает на всех устройствах iOS 6 до iOS 9:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// create a proper annotation view, be lazy and don't use the reuse identifier
MKAnnotationView *view = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"identifier"];
// create a disclosure button for map kit
UIButton *disclosure = [UIButton buttonWithType:UIButtonTypeContactAdd];
[disclosure addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(disclosureTapped)]];
view.rightCalloutAccessoryView = disclosure;
view.enabled = YES;
view.image = [UIImage imageNamed:@"map_pin"];
return view;
}