Search code examples
iosswiftmkmapview

MKMapView's scale is not shown


I'm doing an iOS application. In Xcode 9.1 I create a MKMapView by

let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))
mapView.isUserInteractionEnabled = false
mapView.mapType = .satellite
mapView.showsCompass = false
mapView.showsScale = true
view.addSubview(mapView)

but when I run it in the simulator the scale is not shown and I get three messages in the log:

Could not inset compass from edges 9

Could not inset scale from edge 9

Could not inset legal attribution from corner 4

The compass is not shown (as expected) but it's not shown if I change mapView.showsCompass to trueeither. However, the Legal link is shown. What am I missing here? I'm guessing it's something about the new safe areas introduced with iOS 11, but I fail to see how that is important for a view I want to be covering the whole screen.


Solution

  • In iOS 10 or lower

    As @Paulw11 says, the scale is only shown while zooming by default.

    In iOS 11

    You can use scaleVisibility. https://developer.apple.com/documentation/mapkit/mkscaleview/2890254-scalevisibility

    let scale = MKScaleView(mapView: mapView)
    scale.scaleVisibility = .visible // always visible
    view.addSubview(scale)