I want to be able to zoom my MKMapView to fit it\'s annotations. I have managed to do it using iOS7\'s showAnnotations method. But I would also like to add some
Starting with iOS8, MKMapView has a layoutMargin property. When this is set, centerRegion:, showAnnotations: and all methods that try to fit a rectangle within the map view will take into account the aforementioned layout margins.
If your translucent view is 40 points height and attached to the top of the map view, settings mapView.layoutMargin = UIEdgeInsetMake(40, 0, 0, 0) will do the magic.
If targeting iOS7, the map view uses the top and bottom layout guide of its containing controller to also offset its content. So you could override the topLayoutGuide method of the controller to return the desired length.
class ViewController: UIViewController {
override var topLayoutGuide: UILayoutSupport {
return MapLayoutGuide(length: 40)
}
}
class MapLayoutGuide: NSObject, UILayoutSupport {
var length: CGFloat
init(length: CGFloat) {
self.length = length
super.init()
}
@available(iOS 9.0, *)
var bottomAnchor: NSLayoutYAxisAnchor {
return NSLayoutYAxisAnchor()
}
@available(iOS 9.0, *)
var topAnchor: NSLayoutYAxisAnchor {
return NSLayoutYAxisAnchor()
}
@available(iOS 9.0, *)
var heightAnchor: NSLayoutDimension {
return NSLayoutDimension()
}
}