By default MGLMapView puts the scaleBar view at the top left of the map. I would like to move it to the bottom left, but I am having issues with doing this. Either my NSLayoutConstraint code is wrong, or something else is happening. The scaleBar is still stuck on the top left.
Here is what I have tried:
NSMutableArray *scaleBarConstraints = [[NSMutableArray alloc] init];
[self.mapboxMapView removeConstraints:self.mapboxMapView.scaleBar.constraints];
[scaleBarConstraints addObject:
[NSLayoutConstraint constraintWithItem:self.mapboxMapView.scaleBar
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.mapboxMapView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:8.0 + self.mapboxMapView.contentInset.bottom]];
[scaleBarConstraints addObject:
[NSLayoutConstraint constraintWithItem:self.mapboxMapView.scaleBar
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.mapboxMapView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:8.0 + self.mapboxMapView.contentInset.left]];
[self.mapboxMapView addConstraints:scaleBarConstraints];
Is there another way to do this or have I missed something?
First of all, your first constraint specifies a
NSLayoutRelationGreaterThanOrEqualrelation, so this means that yourscaleBar's bottom edge must be somewhere above the bottom edge of the map view (+ the constant that you specified).This is obviously true when the scale bar is positioned at the top of the map view. So try to replace
NSLayoutRelationGreaterThanOrEqualwithNSLayoutRelationEqualand see if it does the trick.Warning 1:
You're adding the constraints to
self.mapboxMapViewwhich is not the direct superview of thescaleBar. (In the Mapbox library they add the constraints to acontainerView.) This is bad practice. I recommend to useinstead which will automatically add the constraints to the adequate view. (Apple recommends this in their documentation.)
Warning 2:
Generally you shouldn't hack the view hierarchy of another class (that you don't own). You cannot assume that it doesn't create new constraints at some point in time which might override or conflict with your own constraints.