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

Commit

Permalink
Avoid removing content size constraints
Browse files Browse the repository at this point in the history
Also, on iOS 8.0+, use `+[NSLayoutConstraint activateConstraints:]` and `+deactivateConstraints:` to efficiently prevent redundant constraints from lingering.

Fixes #1779.
  • Loading branch information
1ec5 committed Jun 26, 2015
1 parent ea5c56d commit 5ea41a5
Showing 1 changed file with 71 additions and 20 deletions.
91 changes: 71 additions & 20 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ @interface MGLMapView () <UIGestureRecognizerDelegate, GLKViewDelegate, CLLocati
@property (nonatomic) NSOperationQueue *regionChangeDelegateQueue;
@property (nonatomic) UIImageView *compass;
@property (nonatomic) UIImageView *logoBug;
@property (nonatomic) NS_MUTABLE_ARRAY_OF(NSLayoutConstraint *) *logoBugConstraints;
@property (nonatomic) UIButton *attributionButton;
@property (nonatomic) NS_MUTABLE_ARRAY_OF(NSLayoutConstraint *) *attributionButtonConstraints;
@property (nonatomic) UIActionSheet *attributionSheet;
@property (nonatomic) UIPanGestureRecognizer *pan;
@property (nonatomic) UIPinchGestureRecognizer *pinch;
Expand Down Expand Up @@ -281,6 +283,7 @@ - (void)commonInit
_logoBug.accessibilityLabel = @"Mapbox logo";
_logoBug.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_logoBug];
_logoBugConstraints = [NSMutableArray array];

// setup attribution
//
Expand All @@ -289,6 +292,7 @@ - (void)commonInit
[_attributionButton addTarget:self action:@selector(showAttribution) forControlEvents:UIControlEventTouchUpInside];
_attributionButton.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_attributionButton];
_attributionButtonConstraints = [NSMutableArray array];

_attributionSheet = [[UIActionSheet alloc] initWithTitle:@"Mapbox GL for iOS"
delegate:self
Expand Down Expand Up @@ -429,6 +433,11 @@ - (void)dealloc
{
[EAGLContext setCurrentContext:nil];
}

[self.logoBugConstraints removeAllObjects];
self.logoBugConstraints = nil;
[self.attributionButtonConstraints removeAllObjects];
self.attributionButtonConstraints = nil;
}

- (void)setDelegate:(nullable id<MGLMapViewDelegate>)delegate
Expand Down Expand Up @@ -459,15 +468,6 @@ + (BOOL)requiresConstraintBasedLayout
return YES;
}

- (void)didMoveToSuperview
{
[self.compass.superview removeConstraints:self.compass.superview.constraints];
[self.logoBug removeConstraints:self.logoBug.constraints];
[self.attributionButton removeConstraints:self.attributionButton.constraints];

[self setNeedsUpdateConstraints];
}

- (UIViewController *)viewControllerForLayoutGuides
{
// Per -[UIResponder nextResponder] documentation, a UIView’s next responder
Expand Down Expand Up @@ -498,10 +498,19 @@ - (void)updateConstraints
// compass
//
UIView *compassContainer = self.compass.superview;
if ([NSLayoutConstraint respondsToSelector:@selector(deactivateConstraints:)])
{
[NSLayoutConstraint deactivateConstraints:compassContainer.constraints];
}
else
{
[compassContainer removeConstraints:compassContainer.constraints];
}

NSMutableArray *compassContainerConstraints = [NSMutableArray array];
if (viewController)
{
[constraintParentView addConstraint:
[compassContainerConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
Expand All @@ -510,7 +519,7 @@ - (void)updateConstraints
multiplier:1
constant:5]];
}
[constraintParentView addConstraint:
[compassContainerConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
Expand All @@ -519,7 +528,7 @@ - (void)updateConstraints
multiplier:1
constant:5]];

[constraintParentView addConstraint:
[compassContainerConstraints addObject:
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
Expand All @@ -528,7 +537,7 @@ - (void)updateConstraints
multiplier:1
constant:5]];

[compassContainer addConstraint:
[compassContainerConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
Expand All @@ -537,20 +546,37 @@ - (void)updateConstraints
multiplier:1
constant:self.compass.image.size.width]];

[compassContainer addConstraint:
[compassContainerConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:self.compass.image.size.height]];
if ([NSLayoutConstraint respondsToSelector:@selector(activateConstraints:)])
{
[NSLayoutConstraint activateConstraints:compassContainerConstraints];
}
else
{
[compassContainer addConstraints:compassContainerConstraints];
}

// logo bug
//
if ([NSLayoutConstraint respondsToSelector:@selector(deactivateConstraints:)])
{
[NSLayoutConstraint deactivateConstraints:self.logoBugConstraints];
}
else
{
[self.logoBug removeConstraints:self.logoBugConstraints];
}
[self.logoBugConstraints removeAllObjects];
if (viewController)
{
[constraintParentView addConstraint:
[self.logoBugConstraints addObject:
[NSLayoutConstraint constraintWithItem:viewController.bottomLayoutGuide
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
Expand All @@ -559,7 +585,7 @@ - (void)updateConstraints
multiplier:1
constant:8]];
}
[constraintParentView addConstraint:
[self.logoBugConstraints addObject:
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationGreaterThanOrEqual
Expand All @@ -568,20 +594,37 @@ - (void)updateConstraints
multiplier:1
constant:8]];

[constraintParentView addConstraint:
[self.logoBugConstraints addObject:
[NSLayoutConstraint constraintWithItem:self.logoBug
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeLeading
multiplier:1
constant:8]];
if ([NSLayoutConstraint respondsToSelector:@selector(activateConstraints:)])
{
[NSLayoutConstraint activateConstraints:self.logoBugConstraints];
}
else
{
[constraintParentView addConstraints:self.logoBugConstraints];
}

// attribution button
//
if ([NSLayoutConstraint respondsToSelector:@selector(deactivateConstraints:)])
{
[NSLayoutConstraint deactivateConstraints:self.attributionButtonConstraints];
}
else
{
[self.attributionButton removeConstraints:self.attributionButtonConstraints];
}
[self.attributionButtonConstraints removeAllObjects];
if (viewController)
{
[constraintParentView addConstraint:
[self.attributionButtonConstraints addObject:
[NSLayoutConstraint constraintWithItem:viewController.bottomLayoutGuide
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
Expand All @@ -590,7 +633,7 @@ - (void)updateConstraints
multiplier:1
constant:8]];
}
[constraintParentView addConstraint:
[self.attributionButtonConstraints addObject:
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationGreaterThanOrEqual
Expand All @@ -599,14 +642,22 @@ - (void)updateConstraints
multiplier:1
constant:8]];

[constraintParentView addConstraint:
[self.attributionButtonConstraints addObject:
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.attributionButton
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:8]];
if ([NSLayoutConstraint respondsToSelector:@selector(activateConstraints:)])
{
[NSLayoutConstraint activateConstraints:self.attributionButtonConstraints];
}
else
{
[constraintParentView addConstraints:self.attributionButtonConstraints];
}

[super updateConstraints];
}
Expand Down

0 comments on commit 5ea41a5

Please sign in to comment.