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

Fix problems with map view UI layout constraints #6776

Merged
merged 3 commits into from
Nov 15, 2016
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
40 changes: 25 additions & 15 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ - (void)commonInit
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wakeGL:) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wakeGL:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

// set initial position
//
Expand Down Expand Up @@ -639,6 +641,7 @@ - (void)reachabilityChanged:(NSNotification *)notification

- (void)dealloc
{
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_attributionButton removeObserver:self forKeyPath:@"hidden"];

Expand Down Expand Up @@ -760,24 +763,26 @@ - (UIViewController *)viewControllerForLayoutGuides

- (void)updateConstraints
{
// If we have a view controller reference, use its layout guides for our various top & bottom
// views so they don't underlap navigation or tool bars. If we don't have a reference, apply
// constraints against ourself to maintain (albeit less ideal) placement of the subviews.
// 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;
UIView *constraintParentView = (viewController.view ? viewController.view : self);

BOOL useLayoutGuides = viewController.view && viewController.automaticallyAdjustsScrollViewInsets;
UIView *view = useLayoutGuides ? viewController.view : self;

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

[self.compassViewConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self
relatedBy:NSLayoutRelationEqual
toItem:view
attribute:NSLayoutAttributeTop
multiplier:1
constant:5 + self.contentInset.top]];
Expand Down Expand Up @@ -809,17 +814,17 @@ - (void)updateConstraints
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:compassImage.size.height]];
[constraintParentView addConstraints:self.compassViewConstraints];
[view addConstraints:self.compassViewConstraints];

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

[self.logoViewConstraints addObject:
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationGreaterThanOrEqual
relatedBy:NSLayoutRelationEqual
toItem:self.logoView
attribute:NSLayoutAttributeBaseline
multiplier:1
Expand All @@ -833,17 +838,17 @@ - (void)updateConstraints
attribute:NSLayoutAttributeLeading
multiplier:1
constant:8 + self.contentInset.left]];
[constraintParentView addConstraints:self.logoViewConstraints];
[view addConstraints:self.logoViewConstraints];

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

[self.attributionButtonConstraints addObject:
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationGreaterThanOrEqual
relatedBy:NSLayoutRelationEqual
toItem:self.attributionButton
attribute:NSLayoutAttributeBaseline
multiplier:1
Expand All @@ -857,7 +862,7 @@ - (void)updateConstraints
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:8 + self.contentInset.right]];
[constraintParentView addConstraints:self.attributionButtonConstraints];
[view addConstraints:self.attributionButtonConstraints];

[super updateConstraints];
}
Expand Down Expand Up @@ -1064,6 +1069,11 @@ - (void)didMoveToSuperview
[super didMoveToSuperview];
}

- (void)deviceOrientationDidChange:(__unused NSNotification *)notification
{
[self setNeedsLayout];
}

- (void)sleepGL:(__unused NSNotification *)notification
{
MGLAssertIsMainThread();
Expand Down