Search code examples
iosswiftmapkitmkmapviewios11

How to reposition compass of MKMapView?


I want to move the MKMapView compass. I wanted to get a reference for it by something like this:

let compassView = mapView.subviews.filter {$0 is NSClassFromString("MKCompassView")}

However the compiler complains " Use of undeclared type 'NSClassFromString' ". How can I fix this code?


Solution

  • iOS 11

    you should use MKCompassButton, doc explaining the new stuff: WWDC 2017 new MapKit presentation.

    let compassButton = MKCompassButton(mapView:mapView)
    compassButton.frame.origin = CGPoint(x: 20, y: 20)
    compassButton.compassVisibility = .visible
    view.addSubview(compassButton)
    

    iOS < 11

    You might try to use String(describing:), something like:

    if let compassButton = (mapView.subviews.filter { String(describing:$0).contains("MKCompassView") }.first) {
       print(compassButton)
    }