Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 355
mistakeNot Avatar asked Jan 29 '23 20:01

mistakeNot


1 Answers

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)
}
like image 106
Andrea Mugnaini Avatar answered Jan 31 '23 22:01

Andrea Mugnaini