Reputation: 42165
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?
Upvotes: 3
Views: 401
Reputation: 1
To change scaleBar position you can mutate using scaleBarPosition
property on specific place, additional you can use scaleBarMargins
property for margins, for example:
scaleBarMargins = CGPoint(x: scaleBar.frame.origin.x + 'offsetX', y: scaleBar.frame.origin.y + 'offsetY')
Hope it helps!
Upvotes: 0
Reputation: 17261
First of all, your first constraint specifies a NSLayoutRelationGreaterThanOrEqual
relation, so this means that your scaleBar
'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 NSLayoutRelationGreaterThanOrEqual
with NSLayoutRelationEqual
and see if it does the trick.
You're adding the constraints to self.mapboxMapView
which is not the direct superview of the scaleBar
. (In the Mapbox library they add the constraints to a containerView
.) This is bad practice. I recommend to use
[NSLayoutConstraint activateConstraints:scaleBarConstraints];
instead which will automatically add the constraints to the adequate view. (Apple recommends this in their documentation.)
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.
Upvotes: 1