0

Estoy creando una UIViewController con un mapa de MapKit, y necesito copiar el diseño de unos pins hechos en android, como puedo cambiar el font del titulo y subtitulo de las MKAnnotationView para ponerlos igual que en android

Este es mi codigo:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard annotation is MKPointAnnotation else { return nil }

        let identifier = "Annotation"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.image = UIImage(named: "")
            annotationView!.canShowCallout = true
        } else {
            annotationView!.annotation = annotation
            annotationView!.canShowCallout = true
        }

        return annotationView
    }

Mapa de ios Mapa de android

1 respuesta 1

0

Debes sobreescribir el método de selección del pin en una nueva clase:

class Custom: MKPinAnnotationView {
    let titulo: UILabel = UILabel.init(frame:CGRectMake(0, 0, 100, 40))
    let subtitulo: UILabel = UILabel.init(frame:CGRectMake(0, 15, 80, 40))

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(false, animated: animated)
        //Aquí personalizas
        if selected {
            titulo.font = UIFont.init(name: "Rockwell", size: 20.0)//Fuente y tamaño
            titulo.textColor = .green//Color

            subtitulo.font = UIFont.init(name: "Rockwell", size: 20.0)//Fuente y tamaño
            subtitulo.textColor = .green//Color

            //Se agrega los labels al view.
            self.addSubview(titulo)
            self.addSubview(subtitulo)
        }
        else
        {
            titulo.removeFromSuperview()
            subtitulo.removeFromSuperview()
        }
    }
}   

Al momento de pintar los pins utilizas la nueva clase:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var newView = Custom.init(annotation: annotation, reuseIdentifier: "Un Identificador")
    newView.titulo.text = annotation.title ?? "" //Agregas el título al nuevo pin
    newView.subtitulo.text = annotation.subtitle ?? "" //Agregas el subtítulo

    return newView
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.