Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios] fixes #3697 compass, logo and attribution now respects insets (#…
Browse files Browse the repository at this point in the history
…5671)

* [ios] fixes #3697 compass, logo and attribution now respects mapview's content inset

* [ios] fixed a bug which added duplicated constraints instead of updating

* [ios] layout guides are already taken into account in contentInset
  • Loading branch information
frederoni authored Jul 18, 2016
1 parent bdd2d02 commit ede1e71
Showing 1 changed file with 22 additions and 49 deletions.
71 changes: 22 additions & 49 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ @interface MGLMapView () <UIGestureRecognizerDelegate,
@property (nonatomic) GLKView *glView;
@property (nonatomic) UIImageView *glSnapshotView;
@property (nonatomic, readwrite) UIImageView *compassView;
@property (nonatomic) NS_MUTABLE_ARRAY_OF(NSLayoutConstraint *) *compassViewConstraints;
@property (nonatomic, readwrite) UIImageView *logoView;
@property (nonatomic) NS_MUTABLE_ARRAY_OF(NSLayoutConstraint *) *logoViewConstraints;
@property (nonatomic, readwrite) UIButton *attributionButton;
Expand Down Expand Up @@ -451,7 +452,6 @@ - (void)commonInit
// setup compass
//
_compassView = [[UIImageView alloc] initWithImage:self.compassImage];
_compassView.frame = { CGPointZero, _compassView.image.size };
_compassView.alpha = 0;
_compassView.userInteractionEnabled = YES;
[_compassView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleCompassTapGesture:)]];
Expand All @@ -462,6 +462,7 @@ - (void)commonInit
[container addSubview:_compassView];
container.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:container];
_compassViewConstraints = [NSMutableArray array];

// setup interaction
//
Expand Down Expand Up @@ -737,40 +738,29 @@ - (void)updateConstraints
// compass
//
UIView *compassContainer = self.compassView.superview;
[compassContainer removeConstraints:compassContainer.constraints];
[constraintParentView removeConstraints:self.compassViewConstraints];
[self.compassViewConstraints removeAllObjects];

NSMutableArray *compassContainerConstraints = [NSMutableArray array];
if (viewController)
{
[compassContainerConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:viewController.topLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1
constant:5]];
}
[compassContainerConstraints addObject:
[self.compassViewConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self
attribute:NSLayoutAttributeTop
multiplier:1
constant:5]];
constant:5 + self.contentInset.top]];

[compassContainerConstraints addObject:
[self.compassViewConstraints addObject:
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:compassContainer
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:5]];
constant:5 + self.contentInset.right]];

UIImage *compassImage = self.compassView.image;
[compassContainerConstraints addObject:
[self.compassViewConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
Expand All @@ -779,39 +769,29 @@ - (void)updateConstraints
multiplier:1
constant:compassImage.size.width]];

[compassContainerConstraints addObject:
[self.compassViewConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:compassImage.size.height]];
[constraintParentView addConstraints:compassContainerConstraints];
[constraintParentView addConstraints:self.compassViewConstraints];

// logo bug
//
[self.logoView removeConstraints:self.logoViewConstraints];
[constraintParentView removeConstraints:self.logoViewConstraints];
[self.logoViewConstraints removeAllObjects];
if (viewController)
{
[self.logoViewConstraints addObject:
[NSLayoutConstraint constraintWithItem:viewController.bottomLayoutGuide
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.logoView
attribute:NSLayoutAttributeBaseline
multiplier:1
constant:8]];
}

[self.logoViewConstraints addObject:
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.logoView
attribute:NSLayoutAttributeBaseline
multiplier:1
constant:8]];
constant:8 + self.contentInset.bottom]];

[self.logoViewConstraints addObject:
[NSLayoutConstraint constraintWithItem:self.logoView
Expand All @@ -820,32 +800,22 @@ - (void)updateConstraints
toItem:self
attribute:NSLayoutAttributeLeading
multiplier:1
constant:8]];
constant:8 + self.contentInset.left]];
[constraintParentView addConstraints:self.logoViewConstraints];

// attribution button
//
[self.attributionButton removeConstraints:self.attributionButtonConstraints];
[constraintParentView removeConstraints:self.attributionButtonConstraints];
[self.attributionButtonConstraints removeAllObjects];
if (viewController)
{
[self.attributionButtonConstraints addObject:
[NSLayoutConstraint constraintWithItem:viewController.bottomLayoutGuide
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.attributionButton
attribute:NSLayoutAttributeBaseline
multiplier:1
constant:8]];
}

[self.attributionButtonConstraints addObject:
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.attributionButton
attribute:NSLayoutAttributeBaseline
multiplier:1
constant:8]];
constant:8 + self.contentInset.bottom]];

[self.attributionButtonConstraints addObject:
[NSLayoutConstraint constraintWithItem:self
Expand All @@ -854,7 +824,7 @@ - (void)updateConstraints
toItem:self.attributionButton
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:8]];
constant:8 + self.contentInset.right]];
[constraintParentView addConstraints:self.attributionButtonConstraints];

[super updateConstraints];
Expand Down Expand Up @@ -974,6 +944,9 @@ - (void)setContentInset:(UIEdgeInsets)contentInset animated:(BOOL)animated
{
[self didUpdateLocationWithUserTrackingAnimated:animated];
}

// Compass, logo and attribution button constraints needs to be updated.
[self setNeedsUpdateConstraints];
}

/// Returns the frame of inset content within the map view.
Expand Down

0 comments on commit ede1e71

Please sign in to comment.