Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 11 additions & 41 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,8 @@ - (void)commonInit
_compassView.accessibilityTraits = UIAccessibilityTraitButton;
_compassView.accessibilityLabel = NSLocalizedStringWithDefaultValue(@"COMPASS_A11Y_LABEL", nil, nil, @"Compass", @"Accessibility label");
_compassView.accessibilityHint = NSLocalizedStringWithDefaultValue(@"COMPASS_A11Y_HINT", nil, nil, @"Rotates the map to face due north", @"Accessibility hint");
UIView *container = [[UIView alloc] initWithFrame:CGRectZero];
[container addSubview:_compassView];
container.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:container];
_compassView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_compassView];
_compassViewConstraints = [NSMutableArray array];

// setup interaction
Expand Down Expand Up @@ -763,26 +761,16 @@ - (UIViewController *)viewControllerForLayoutGuides

- (void)updateConstraints
{
// If we have a view controller reference and its automaticallyAdjustsScrollViewInsets
// is set to YES, use its view as the parent for constraints. -[MGLMapView adjustContentInset]
// already take top and bottom layout guides into account. If we don't have a reference, apply
// constraints against ourself to maintain placement of the subviews.
//
UIViewController *viewController = self.viewControllerForLayoutGuides;
BOOL useLayoutGuides = viewController.view && viewController.automaticallyAdjustsScrollViewInsets;
UIView *view = useLayoutGuides ? viewController.view : self;

// compass
//
UIView *compassContainer = self.compassView.superview;
[view removeConstraints:self.compassViewConstraints];
[self removeConstraints:self.compassViewConstraints];
[self.compassViewConstraints removeAllObjects];

[self.compassViewConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
[NSLayoutConstraint constraintWithItem:self.compassView

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the compass container view isn’t constrained to anything anymore? Do we still need it?

@incanus, do you remember why we stuck the compass view inside a container view in the first place?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@incanus, do you remember why we stuck the compass view inside a container view in the first place?

Indeed! When you don't have it in a container, but constrain it to the corner using margins, it jiggles as the constraints attempt to keep up with the actual margins as they change. You'd think this would not be the case for a round image, but it is. So constraining the container keeps it solidly in place, changing only for device rotation and superview frame changes, while letting the round part rotate about its center, which is firmly constrained to the (from its perspective) unchanging container.

So, if you get rid of the container, make sure the compass doesn't jiggle. It may have been a major iOS version-specific bug, so we should check all supported versions.

mapbox/DEPRECATED-mapbox-ios-sdk@f7fdc9d#diff-cc99abd5af5db8f48b362bcac3e8c307R514

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you don't have it in a container, but constrain it to the corner using margins, it jiggles as the constraints attempt to keep up with the actual margins as they change.

Indeed that might've been the case in the early retina days. I've seen the same problems when using half pixels (compass is 34.5 w/h).

I removed the container view and the w/h constraints because UIImageView has intrinsic content size when initialized with an image or sizeToFit has been called and I can't see any regression.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This simplification looks great, but before merging, can we get testing on iOS 8 (and ideally iOS 7, if possible, since it's still supported)?

attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:view
toItem:self
attribute:NSLayoutAttributeTop
multiplier:1
constant:5 + self.contentInset.top]];
Expand All @@ -791,34 +779,16 @@ - (void)updateConstraints
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:compassContainer
toItem:self.compassView
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:5 + self.contentInset.right]];

UIImage *compassImage = self.compassView.image;
[self.compassViewConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:compassImage.size.width]];

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

// logo bug
//
[view removeConstraints:self.logoViewConstraints];
[self removeConstraints:self.logoViewConstraints];
[self.logoViewConstraints removeAllObjects];

[self.logoViewConstraints addObject:
Expand All @@ -838,11 +808,11 @@ - (void)updateConstraints
attribute:NSLayoutAttributeLeading
multiplier:1
constant:8 + self.contentInset.left]];
[view addConstraints:self.logoViewConstraints];
[self addConstraints:self.logoViewConstraints];

// attribution button
//
[view removeConstraints:self.attributionButtonConstraints];
[self removeConstraints:self.attributionButtonConstraints];
[self.attributionButtonConstraints removeAllObjects];

[self.attributionButtonConstraints addObject:
Expand All @@ -862,7 +832,7 @@ - (void)updateConstraints
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:8 + self.contentInset.right]];
[view addConstraints:self.attributionButtonConstraints];
[self addConstraints:self.attributionButtonConstraints];

[super updateConstraints];
}
Expand Down