From bb6aa5dbb322aae536b8d507dc7a94e516cbe7d3 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Fri, 8 Nov 2019 16:47:28 +0800 Subject: [PATCH 01/17] [ios] fix bold text reset view's bounds issue --- platform/ios/src/MGLMapView.mm | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 9b4ef8ff2d..a464dc3b01 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -889,8 +889,11 @@ - (void)updateConstraintsForOrnament:(UIView *)view } if (!CGSizeEqualToSize(size, CGSizeZero)) { - [updatedConstraints addObject:[view.widthAnchor constraintEqualToConstant:size.width]]; - [updatedConstraints addObject:[view.heightAnchor constraintEqualToConstant:size.height]]; + NSLayoutConstraint *widthConstraint = [view.widthAnchor constraintEqualToConstant:size.width]; + widthConstraint.identifier = @"width"; + NSLayoutConstraint *heightConstraint = [view.heightAnchor constraintEqualToConstant:size.height]; + heightConstraint.identifier = @"height"; + [updatedConstraints addObjectsFromArray:@[widthConstraint,heightConstraint]]; } [NSLayoutConstraint deactivateConstraints:constraints]; @@ -912,7 +915,7 @@ - (void)installCompassViewConstraints { [self updateConstraintsForOrnament:self.compassView constraints:self.compassViewConstraints position:self.compassViewPosition - size:self.compassView.bounds.size + size:[self sizeForOrnament:self.compassView constraints:self.compassViewConstraints] margins:self.compassViewMargins]; } @@ -930,7 +933,7 @@ - (void)installLogoViewConstraints { [self updateConstraintsForOrnament:self.logoView constraints:self.logoViewConstraints position:self.logoViewPosition - size:self.logoView.bounds.size + size:[self sizeForOrnament:self.logoView constraints:self.logoViewConstraints] margins:self.logoViewMargins]; } @@ -939,10 +942,31 @@ - (void)installAttributionButtonConstraints { [self updateConstraintsForOrnament:self.attributionButton constraints:self.attributionButtonConstraints position:self.attributionButtonPosition - size:self.attributionButton.bounds.size + size:[self sizeForOrnament:self.attributionButton constraints:self.attributionButtonConstraints] margins:self.attributionButtonMargins]; } +- (CGSize)sizeForOrnament:(UIView *)view + constraints:(NSMutableArray *)constraints { + // avoid regenerating size constraints + CGSize size; + if(constraints && constraints.count > 0) { + for (NSLayoutConstraint * constraint in constraints) { + if([constraint.identifier isEqualToString:@"width"]) { + size.width = constraint.constant; + } + else if ([constraint.identifier isEqualToString:@"height"]) { + size.height = constraint.constant; + } + } + } + else { + size = view.bounds.size; + } + + return size; +} + - (BOOL)isOpaque { return _opaque; From 71b644e23bf3a334d84cb7b00de93c9683345b14 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Fri, 8 Nov 2019 16:53:55 +0800 Subject: [PATCH 02/17] [ios]add change log --- platform/ios/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index 06eef12ce1..ab7c67b783 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -2,6 +2,10 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONTRIBUTING.md](../../CONTRIBUTING.md) to get started. +## master + +* Fix ornaments' view constraints bug when devices change to bold-text mode. ([#57](https://github.com/mapbox/mapbox-gl-native-ios/pull/57)) + ## 5.5.0 ### Performance improvements From 710b19be24b3b04cf28edb9e65d8add3fb03f6b2 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Fri, 8 Nov 2019 19:44:19 +0800 Subject: [PATCH 03/17] [ios] Add a value of the maximum width ratio for scale bar, which relative to the map view's frame --- platform/ios/src/MGLMapView.h | 7 +++++++ platform/ios/src/MGLMapView.mm | 19 +++++++++++++++++++ platform/ios/src/MGLScaleBar.h | 9 +++++++++ platform/ios/src/MGLScaleBar.mm | 17 ++++++++++++++++- 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/platform/ios/src/MGLMapView.h b/platform/ios/src/MGLMapView.h index 8f27adee9e..c5e631548a 100644 --- a/platform/ios/src/MGLMapView.h +++ b/platform/ios/src/MGLMapView.h @@ -329,6 +329,13 @@ MGL_EXPORT */ @property (nonatomic, assign) CGPoint scaleBarMargins; +/** + The maximum width ratio of the scale bar relative to the map view's frame. + This value is limited from 0.2 to 1. + Default is 0.5(half of map view's width). + */ +@property (nonatomic, assign) CGFloat scaleBarMaximumWidthRatio; + /** A control indicating the map’s direction and allowing the user to manipulate the direction, positioned in the upper-right corner. diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index a464dc3b01..8155282373 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -800,9 +800,20 @@ - (void)setScaleBarPosition:(MGLOrnamentPosition)scaleBarPosition { - (void)setScaleBarMargins:(CGPoint)scaleBarMargins { MGLLogDebug(@"Setting scaleBarMargins: (x:%f, y:%f)", scaleBarMargins.x, scaleBarMargins.y); _scaleBarMargins = scaleBarMargins; + if([self.scaleBar isKindOfClass:[MGLScaleBar class]]) { + ((MGLScaleBar *)self.scaleBar).margins = scaleBarMargins; + } [self installScaleBarConstraints]; } +- (void)setScaleBarMaximumWidthRatio:(CGFloat)scaleBarMaximumWidthRatio { + _scaleBarMaximumWidthRatio = scaleBarMaximumWidthRatio; + if([self.scaleBar isKindOfClass:[MGLScaleBar class]]) { + ((MGLScaleBar *)self.scaleBar).maximumWidthRatio = scaleBarMaximumWidthRatio; + } + [self updateScaleBar]; +} + - (void)setCompassViewPosition:(MGLOrnamentPosition)compassViewPosition { MGLLogDebug(@"Setting compassViewPosition: %lu", compassViewPosition); _compassViewPosition = compassViewPosition; @@ -873,18 +884,26 @@ - (void)updateConstraintsForOrnament:(UIView *)view case MGLOrnamentPositionTopLeft: [updatedConstraints addObject:[view.topAnchor constraintEqualToAnchor:self.mgl_safeTopAnchor constant:margins.y + inset.top]]; [updatedConstraints addObject:[view.leadingAnchor constraintEqualToAnchor:self.mgl_safeLeadingAnchor constant:margins.x + inset.left]]; + if(CGSizeEqualToSize(size, CGSizeZero)) + [updatedConstraints addObject:[view.trailingAnchor constraintGreaterThanOrEqualToAnchor:self.mgl_safeTrailingAnchor constant:margins.x + inset.left]]; break; case MGLOrnamentPositionTopRight: [updatedConstraints addObject:[view.topAnchor constraintEqualToAnchor:self.mgl_safeTopAnchor constant:margins.y + inset.top]]; [updatedConstraints addObject:[self.mgl_safeTrailingAnchor constraintEqualToAnchor:view.trailingAnchor constant:margins.x + inset.right]]; + if(CGSizeEqualToSize(size, CGSizeZero)) + [updatedConstraints addObject:[view.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.mgl_safeLeadingAnchor constant:margins.x + inset.right]]; break; case MGLOrnamentPositionBottomLeft: [updatedConstraints addObject:[self.mgl_safeBottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:margins.y + inset.bottom]]; [updatedConstraints addObject:[view.leadingAnchor constraintEqualToAnchor:self.mgl_safeLeadingAnchor constant:margins.x + inset.left]]; + if(CGSizeEqualToSize(size, CGSizeZero)) + [updatedConstraints addObject:[view.trailingAnchor constraintGreaterThanOrEqualToAnchor:self.mgl_safeTrailingAnchor constant:margins.x + inset.left]]; break; case MGLOrnamentPositionBottomRight: [updatedConstraints addObject:[self.mgl_safeBottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:margins.y + inset.bottom]]; [updatedConstraints addObject: [self.mgl_safeTrailingAnchor constraintEqualToAnchor:view.trailingAnchor constant:margins.x + inset.right]]; + if(CGSizeEqualToSize(size, CGSizeZero)) + [updatedConstraints addObject:[view.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.mgl_safeLeadingAnchor constant:margins.x + inset.right]]; break; } diff --git a/platform/ios/src/MGLScaleBar.h b/platform/ios/src/MGLScaleBar.h index 77fd6736b5..c6d2966f7c 100644 --- a/platform/ios/src/MGLScaleBar.h +++ b/platform/ios/src/MGLScaleBar.h @@ -6,4 +6,13 @@ // Sets the scale and redraws the scale bar @property (nonatomic, assign) CLLocationDistance metersPerPoint; +/* + The maximum width ratio of the scale bar relative to the map view's frame. + This value is limited from 0.2 to 1. + Default is 0.5(half of map view's width). + */ +@property (nonatomic, assign) CGFloat maximumWidthRatio; + +@property (nonatomic, assign) CGPoint margins; + @end diff --git a/platform/ios/src/MGLScaleBar.mm b/platform/ios/src/MGLScaleBar.mm index 3efa80013f..cfb6596b98 100644 --- a/platform/ios/src/MGLScaleBar.mm +++ b/platform/ios/src/MGLScaleBar.mm @@ -142,6 +142,7 @@ - (instancetype)initWithFrame:(CGRect)frame { - (void)commonInit { _size = CGSizeZero; + _maximumWidthRatio = 0.5; _primaryColor = [UIColor colorWithRed:18.0/255.0 green:45.0/255.0 blue:17.0/255.0 alpha:1]; _secondaryColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1]; @@ -229,7 +230,21 @@ - (CGFloat)actualWidth { - (CGFloat)maximumWidth { // TODO: Consider taking Scale Bar margins into account here. CGFloat fullWidth = CGRectGetWidth(self.superview.bounds); - return floorf(fullWidth / 2); + + if(self.maximumWidthRatio < 0.2) { + self.maximumWidthRatio = 0.2; + } + else if (self.maximumWidthRatio > 1) { + self.maximumWidthRatio = 1; + } + + CGFloat result = floorf(fullWidth * self.maximumWidthRatio); + return result > fullWidth - (self.margins.x * 2) ? fullWidth - (self.margins.x * 2) : result; +} + +- (void)setMaximumWidthRatio:(CGFloat)maximumWidthRatio { + _maximumWidthRatio = maximumWidthRatio; + self.recalculateSize = YES; } - (CGFloat)unitsPerPoint { From 6cfb5ec0cc3d70a08b3a479b3550d5e4dae94480 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Wed, 20 Nov 2019 15:24:42 +0800 Subject: [PATCH 04/17] change to 0.1 --- platform/ios/src/MGLMapView.h | 2 +- platform/ios/src/MGLMapView.mm | 9 +-------- platform/ios/src/MGLScaleBar.mm | 4 ++-- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/platform/ios/src/MGLMapView.h b/platform/ios/src/MGLMapView.h index c5e631548a..07f20edd9e 100644 --- a/platform/ios/src/MGLMapView.h +++ b/platform/ios/src/MGLMapView.h @@ -331,7 +331,7 @@ MGL_EXPORT /** The maximum width ratio of the scale bar relative to the map view's frame. - This value is limited from 0.2 to 1. + This value is limited from 0.1 to 1. Default is 0.5(half of map view's width). */ @property (nonatomic, assign) CGFloat scaleBarMaximumWidthRatio; diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 8155282373..385fdc8bd4 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -576,6 +576,7 @@ - (void)commonInit _scaleBarConstraints = [NSMutableArray array]; _scaleBarPosition = MGLOrnamentPositionTopLeft; _scaleBarMargins = MGLOrnamentDefaultPositionOffset; + _scaleBar.margins = MGLOrnamentDefaultPositionOffset; [self installConstraints]; @@ -884,26 +885,18 @@ - (void)updateConstraintsForOrnament:(UIView *)view case MGLOrnamentPositionTopLeft: [updatedConstraints addObject:[view.topAnchor constraintEqualToAnchor:self.mgl_safeTopAnchor constant:margins.y + inset.top]]; [updatedConstraints addObject:[view.leadingAnchor constraintEqualToAnchor:self.mgl_safeLeadingAnchor constant:margins.x + inset.left]]; - if(CGSizeEqualToSize(size, CGSizeZero)) - [updatedConstraints addObject:[view.trailingAnchor constraintGreaterThanOrEqualToAnchor:self.mgl_safeTrailingAnchor constant:margins.x + inset.left]]; break; case MGLOrnamentPositionTopRight: [updatedConstraints addObject:[view.topAnchor constraintEqualToAnchor:self.mgl_safeTopAnchor constant:margins.y + inset.top]]; [updatedConstraints addObject:[self.mgl_safeTrailingAnchor constraintEqualToAnchor:view.trailingAnchor constant:margins.x + inset.right]]; - if(CGSizeEqualToSize(size, CGSizeZero)) - [updatedConstraints addObject:[view.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.mgl_safeLeadingAnchor constant:margins.x + inset.right]]; break; case MGLOrnamentPositionBottomLeft: [updatedConstraints addObject:[self.mgl_safeBottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:margins.y + inset.bottom]]; [updatedConstraints addObject:[view.leadingAnchor constraintEqualToAnchor:self.mgl_safeLeadingAnchor constant:margins.x + inset.left]]; - if(CGSizeEqualToSize(size, CGSizeZero)) - [updatedConstraints addObject:[view.trailingAnchor constraintGreaterThanOrEqualToAnchor:self.mgl_safeTrailingAnchor constant:margins.x + inset.left]]; break; case MGLOrnamentPositionBottomRight: [updatedConstraints addObject:[self.mgl_safeBottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:margins.y + inset.bottom]]; [updatedConstraints addObject: [self.mgl_safeTrailingAnchor constraintEqualToAnchor:view.trailingAnchor constant:margins.x + inset.right]]; - if(CGSizeEqualToSize(size, CGSizeZero)) - [updatedConstraints addObject:[view.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.mgl_safeLeadingAnchor constant:margins.x + inset.right]]; break; } diff --git a/platform/ios/src/MGLScaleBar.mm b/platform/ios/src/MGLScaleBar.mm index cfb6596b98..a377d8a4e0 100644 --- a/platform/ios/src/MGLScaleBar.mm +++ b/platform/ios/src/MGLScaleBar.mm @@ -231,8 +231,8 @@ - (CGFloat)maximumWidth { // TODO: Consider taking Scale Bar margins into account here. CGFloat fullWidth = CGRectGetWidth(self.superview.bounds); - if(self.maximumWidthRatio < 0.2) { - self.maximumWidthRatio = 0.2; + if(self.maximumWidthRatio < 0.1) { + self.maximumWidthRatio = 0.1; } else if (self.maximumWidthRatio > 1) { self.maximumWidthRatio = 1; From 9a77870ac6d851b25aaa2610894d3b2c49d1ddd7 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Wed, 20 Nov 2019 15:34:09 +0800 Subject: [PATCH 05/17] update scale bar when view refresh --- platform/ios/src/MGLScaleBar.h | 2 +- platform/ios/src/MGLScaleBar.mm | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/platform/ios/src/MGLScaleBar.h b/platform/ios/src/MGLScaleBar.h index c6d2966f7c..a613fb54d2 100644 --- a/platform/ios/src/MGLScaleBar.h +++ b/platform/ios/src/MGLScaleBar.h @@ -8,7 +8,7 @@ /* The maximum width ratio of the scale bar relative to the map view's frame. - This value is limited from 0.2 to 1. + This value is limited from 0.1 to 1. Default is 0.5(half of map view's width). */ @property (nonatomic, assign) CGFloat maximumWidthRatio; diff --git a/platform/ios/src/MGLScaleBar.mm b/platform/ios/src/MGLScaleBar.mm index a377d8a4e0..d5e5fbeb2b 100644 --- a/platform/ios/src/MGLScaleBar.mm +++ b/platform/ios/src/MGLScaleBar.mm @@ -244,7 +244,11 @@ - (CGFloat)maximumWidth { - (void)setMaximumWidthRatio:(CGFloat)maximumWidthRatio { _maximumWidthRatio = maximumWidthRatio; + + [self updateVisibility]; + self.recalculateSize = YES; + [self invalidateIntrinsicContentSize]; } - (CGFloat)unitsPerPoint { From 47c99621f2eed003e42459740dd6e26668cc933d Mon Sep 17 00:00:00 2001 From: m-stephen Date: Wed, 20 Nov 2019 16:06:46 +0800 Subject: [PATCH 06/17] Update CHANGELOG.md update change log --- platform/ios/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index 8bfcd06d0b..7b7b0ac361 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -5,7 +5,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT ## master * Fix ornaments' view constraints bug when devices change to bold-text mode. ([#57](https://github.com/mapbox/mapbox-gl-native-ios/pull/57)) -* Add a `maximumWidthRatio` property for customizing maximum width of scale bar. ([#52](https://github.com/mapbox/mapbox-gl-native-ios/issues/52)) +* Add a `maximumWidthRatio` property for customizing maximum width of scale bar. ([#65](https://github.com/mapbox/mapbox-gl-native-ios/pull/65)) ## 5.5.0 - November 5, 2019 From 9e5f32aadbd4ec673cc428c64b1562128624dce8 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Mon, 16 Dec 2019 14:36:54 +0800 Subject: [PATCH 07/17] add tests --- platform/ios/test/MGLMapViewScaleBarTests.m | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/platform/ios/test/MGLMapViewScaleBarTests.m b/platform/ios/test/MGLMapViewScaleBarTests.m index b4f81ef62b..a5cd85a7a1 100644 --- a/platform/ios/test/MGLMapViewScaleBarTests.m +++ b/platform/ios/test/MGLMapViewScaleBarTests.m @@ -67,4 +67,22 @@ - (void)testDirectlySettingScaleBarViewHiddenProperty { XCTAssertGreaterThan(scaleBar.alpha, 0); XCTAssertFalse(CGSizeEqualToSize(scaleBar.intrinsicContentSize, CGSizeZero)); -}@end +} + +- (void)testChangeScaleBarSize { + self.mapView.scaleBarMaximumWidthRatio = 0.5; + UIView *scaleBar = self.mapView.scaleBar; + scaleBar.hidden = NO; + + self.mapView.zoomLevel = 15; + [self.mapView layoutIfNeeded]; + + XCTAssertLessThanOrEqual(scaleBar.frame.size.width, UIScreen.mainScreen.bounds.size.width/2); + + self.mapView.zoomLevel = 5; + [self.mapView layoutIfNeeded]; + + XCTAssertLessThanOrEqual(scaleBar.frame.size.width, UIScreen.mainScreen.bounds.size.width/2); +} + +@end From eb46bd941dfc28fe61151659dba4795065210a28 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Tue, 17 Dec 2019 11:11:54 +0800 Subject: [PATCH 08/17] add dynamic change test --- platform/ios/test/MGLMapViewScaleBarTests.m | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/platform/ios/test/MGLMapViewScaleBarTests.m b/platform/ios/test/MGLMapViewScaleBarTests.m index a5cd85a7a1..b6f7562861 100644 --- a/platform/ios/test/MGLMapViewScaleBarTests.m +++ b/platform/ios/test/MGLMapViewScaleBarTests.m @@ -77,12 +77,29 @@ - (void)testChangeScaleBarSize { self.mapView.zoomLevel = 15; [self.mapView layoutIfNeeded]; - XCTAssertLessThanOrEqual(scaleBar.frame.size.width, UIScreen.mainScreen.bounds.size.width/2); + XCTAssertLessThanOrEqual(scaleBar.frame.size.width, self.mapView.frame.size.width/2); self.mapView.zoomLevel = 5; [self.mapView layoutIfNeeded]; - XCTAssertLessThanOrEqual(scaleBar.frame.size.width, UIScreen.mainScreen.bounds.size.width/2); + XCTAssertLessThanOrEqual(scaleBar.frame.size.width, self.mapView.frame.size.width/2); +} + +- (void)testChangeScaleBarSize { + self.mapView.scaleBarMaximumWidthRatio = 0.5; + UIView *scaleBar = self.mapView.scaleBar; + scaleBar.hidden = NO; + + self.mapView.zoomLevel = 8; + [self.mapView layoutIfNeeded]; + + XCTAssertLessThanOrEqual(scaleBar.frame.size.width, self.mapView.frame.size.width/2); + + CGRect frame = self.mapView.frame; + frame.size = CGSizeMake(frame.size.width/2, frame.size.height); + self.mapView.frame = frame; + + XCTAssertLessThanOrEqual(scaleBar.frame.size.width, self.mapView.frame.size.width/2); } @end From de75d0805aa4121815bdff72989bf715c686dcff Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Tue, 17 Dec 2019 11:47:38 +0800 Subject: [PATCH 09/17] fix test error --- platform/ios/test/MGLMapViewScaleBarTests.m | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/platform/ios/test/MGLMapViewScaleBarTests.m b/platform/ios/test/MGLMapViewScaleBarTests.m index b6f7562861..ebb7173883 100644 --- a/platform/ios/test/MGLMapViewScaleBarTests.m +++ b/platform/ios/test/MGLMapViewScaleBarTests.m @@ -79,20 +79,8 @@ - (void)testChangeScaleBarSize { XCTAssertLessThanOrEqual(scaleBar.frame.size.width, self.mapView.frame.size.width/2); - self.mapView.zoomLevel = 5; + self.mapView.zoomLevel = 10; [self.mapView layoutIfNeeded]; - - XCTAssertLessThanOrEqual(scaleBar.frame.size.width, self.mapView.frame.size.width/2); -} - -- (void)testChangeScaleBarSize { - self.mapView.scaleBarMaximumWidthRatio = 0.5; - UIView *scaleBar = self.mapView.scaleBar; - scaleBar.hidden = NO; - - self.mapView.zoomLevel = 8; - [self.mapView layoutIfNeeded]; - XCTAssertLessThanOrEqual(scaleBar.frame.size.width, self.mapView.frame.size.width/2); CGRect frame = self.mapView.frame; From a44f036511957085b466a067cee3add786baed96 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Tue, 17 Dec 2019 14:05:45 +0800 Subject: [PATCH 10/17] need recalculate size --- platform/ios/src/MGLMapView.mm | 1 + platform/ios/src/MGLScaleBar.h | 2 ++ platform/ios/src/MGLScaleBar.mm | 4 ++++ 3 files changed, 7 insertions(+) diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 1c9e19d161..953293f24a 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -1010,6 +1010,7 @@ - (void)layoutSubviews // `_mbglMap->setSize()` just below that triggers rendering update which triggers // another scale bar update which causes a rendering update loop and a major performace // degradation. + [(MGLScaleBar *)self.scaleBar setNeedsRecalculateSize]; [self.scaleBar invalidateIntrinsicContentSize]; [self adjustContentInset]; diff --git a/platform/ios/src/MGLScaleBar.h b/platform/ios/src/MGLScaleBar.h index a613fb54d2..0016b4273e 100644 --- a/platform/ios/src/MGLScaleBar.h +++ b/platform/ios/src/MGLScaleBar.h @@ -15,4 +15,6 @@ @property (nonatomic, assign) CGPoint margins; +- (void)setNeedsRecalculateSize; + @end diff --git a/platform/ios/src/MGLScaleBar.mm b/platform/ios/src/MGLScaleBar.mm index d5e5fbeb2b..8ab66ca574 100644 --- a/platform/ios/src/MGLScaleBar.mm +++ b/platform/ios/src/MGLScaleBar.mm @@ -316,6 +316,10 @@ - (void)setMetersPerPoint:(CLLocationDistance)metersPerPoint { [self invalidateIntrinsicContentSize]; } +- (void)setNeedsRecalculateSize { + self.recalculateSize = YES; +} + - (CGSize)intrinsicContentSize { // Size is calculated elsewhere - since intrinsicContentSize is part of the // constraint system, this should be done in updateConstraints From bad60c74bb212b9a29a1bac9427c096a0b6a13c6 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Tue, 17 Dec 2019 15:30:13 +0800 Subject: [PATCH 11/17] update tests --- platform/ios/src/MGLScaleBar.mm | 2 ++ platform/ios/test/MGLMapViewScaleBarTests.m | 12 +++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/platform/ios/src/MGLScaleBar.mm b/platform/ios/src/MGLScaleBar.mm index 8ab66ca574..ed38378cfa 100644 --- a/platform/ios/src/MGLScaleBar.mm +++ b/platform/ios/src/MGLScaleBar.mm @@ -318,6 +318,8 @@ - (void)setMetersPerPoint:(CLLocationDistance)metersPerPoint { - (void)setNeedsRecalculateSize { self.recalculateSize = YES; + [self setNeedsUpdateConstraints]; + [self updateConstraintsIfNeeded]; } - (CGSize)intrinsicContentSize { diff --git a/platform/ios/test/MGLMapViewScaleBarTests.m b/platform/ios/test/MGLMapViewScaleBarTests.m index ebb7173883..7f76447a5a 100644 --- a/platform/ios/test/MGLMapViewScaleBarTests.m +++ b/platform/ios/test/MGLMapViewScaleBarTests.m @@ -58,7 +58,7 @@ - (void)testDirectlySettingScaleBarViewHiddenProperty { // ... but triggering any camera event will update it. self.mapView.zoomLevel = 1; [self.mapView layoutIfNeeded]; - + XCTAssertTrue(CGSizeEqualToSize(scaleBar.intrinsicContentSize, CGSizeZero)); XCTAssertEqual(scaleBar.alpha, 0); @@ -69,7 +69,7 @@ - (void)testDirectlySettingScaleBarViewHiddenProperty { XCTAssertFalse(CGSizeEqualToSize(scaleBar.intrinsicContentSize, CGSizeZero)); } -- (void)testChangeScaleBarSize { +- (void)testScaleBarSizeChanged { self.mapView.scaleBarMaximumWidthRatio = 0.5; UIView *scaleBar = self.mapView.scaleBar; scaleBar.hidden = NO; @@ -77,17 +77,19 @@ - (void)testChangeScaleBarSize { self.mapView.zoomLevel = 15; [self.mapView layoutIfNeeded]; - XCTAssertLessThanOrEqual(scaleBar.frame.size.width, self.mapView.frame.size.width/2); + XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2); self.mapView.zoomLevel = 10; [self.mapView layoutIfNeeded]; - XCTAssertLessThanOrEqual(scaleBar.frame.size.width, self.mapView.frame.size.width/2); + XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2); CGRect frame = self.mapView.frame; frame.size = CGSizeMake(frame.size.width/2, frame.size.height); self.mapView.frame = frame; + [self.mapView setNeedsLayout]; + [self.mapView layoutIfNeeded]; - XCTAssertLessThanOrEqual(scaleBar.frame.size.width, self.mapView.frame.size.width/2); + XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2); } @end From 6214660ade0591603e7f4f717d6bc3d2c00bf694 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Tue, 17 Dec 2019 17:03:08 +0800 Subject: [PATCH 12/17] modify test --- platform/ios/test/MGLMapViewScaleBarTests.m | 1 - 1 file changed, 1 deletion(-) diff --git a/platform/ios/test/MGLMapViewScaleBarTests.m b/platform/ios/test/MGLMapViewScaleBarTests.m index 7f76447a5a..f4437fa221 100644 --- a/platform/ios/test/MGLMapViewScaleBarTests.m +++ b/platform/ios/test/MGLMapViewScaleBarTests.m @@ -86,7 +86,6 @@ - (void)testScaleBarSizeChanged { CGRect frame = self.mapView.frame; frame.size = CGSizeMake(frame.size.width/2, frame.size.height); self.mapView.frame = frame; - [self.mapView setNeedsLayout]; [self.mapView layoutIfNeeded]; XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2); From cfb5828abf36451a0a09b8812437e90de8916af2 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Tue, 17 Dec 2019 17:49:31 +0800 Subject: [PATCH 13/17] update test comment --- platform/ios/test/MGLMapViewScaleBarTests.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/platform/ios/test/MGLMapViewScaleBarTests.m b/platform/ios/test/MGLMapViewScaleBarTests.m index f4437fa221..27199a816c 100644 --- a/platform/ios/test/MGLMapViewScaleBarTests.m +++ b/platform/ios/test/MGLMapViewScaleBarTests.m @@ -88,7 +88,12 @@ - (void)testScaleBarSizeChanged { self.mapView.frame = frame; [self.mapView layoutIfNeeded]; - XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2); + // The bar maximum bar width can only limit the bar's total width. + // Since we should include some space for last label width, the maximum last label width is 30. + // However, the last label cannot be out of bounds by constraints. + CGFloat scaleBarLabelWidthHint = 30.0f; + + XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2 + scaleBarLabelWidthHint); } @end From b570d0e23f998fe6ce3079c6885d8cf23a29c9a7 Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Tue, 17 Dec 2019 17:55:48 +0800 Subject: [PATCH 14/17] update comment --- platform/ios/test/MGLMapViewScaleBarTests.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platform/ios/test/MGLMapViewScaleBarTests.m b/platform/ios/test/MGLMapViewScaleBarTests.m index 27199a816c..79aa99bd6d 100644 --- a/platform/ios/test/MGLMapViewScaleBarTests.m +++ b/platform/ios/test/MGLMapViewScaleBarTests.m @@ -89,8 +89,8 @@ - (void)testScaleBarSizeChanged { [self.mapView layoutIfNeeded]; // The bar maximum bar width can only limit the bar's total width. - // Since we should include some space for last label width, the maximum last label width is 30. - // However, the last label cannot be out of bounds by constraints. + // Sometimes we should include some space for last label width, the maximum last label width is 30. + // We add this hint value to avoid testing failed when need a extra space for last label. CGFloat scaleBarLabelWidthHint = 30.0f; XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2 + scaleBarLabelWidthHint); From 2b6f21bf67a45c2e73ef58a247f1b8262f88006a Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Wed, 18 Dec 2019 10:44:03 +0800 Subject: [PATCH 15/17] add some extra tests --- platform/ios/src/MGLMapView.mm | 1 + platform/ios/test/MGLMapViewScaleBarTests.m | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 953293f24a..d17b37c25f 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -811,6 +811,7 @@ - (void)setScaleBarMaximumWidthRatio:(CGFloat)scaleBarMaximumWidthRatio { _scaleBarMaximumWidthRatio = scaleBarMaximumWidthRatio; if([self.scaleBar isKindOfClass:[MGLScaleBar class]]) { ((MGLScaleBar *)self.scaleBar).maximumWidthRatio = scaleBarMaximumWidthRatio; + [(MGLScaleBar *)self.scaleBar setNeedsRecalculateSize]; } [self updateScaleBar]; } diff --git a/platform/ios/test/MGLMapViewScaleBarTests.m b/platform/ios/test/MGLMapViewScaleBarTests.m index 79aa99bd6d..5cc64a2d01 100644 --- a/platform/ios/test/MGLMapViewScaleBarTests.m +++ b/platform/ios/test/MGLMapViewScaleBarTests.m @@ -70,6 +70,12 @@ - (void)testDirectlySettingScaleBarViewHiddenProperty { } - (void)testScaleBarSizeChanged { + + // The bar maximum bar width can only limit the bar's total width. + // Sometimes we should include some space for last label width, the maximum last label width is 30. + // We add this hint value to avoid testing failed when need a extra space for last label. + CGFloat scaleBarLastLabelWidthHint = 30.0f; + self.mapView.scaleBarMaximumWidthRatio = 0.5; UIView *scaleBar = self.mapView.scaleBar; scaleBar.hidden = NO; @@ -77,23 +83,22 @@ - (void)testScaleBarSizeChanged { self.mapView.zoomLevel = 15; [self.mapView layoutIfNeeded]; - XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2); + XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2 + scaleBarLastLabelWidthHint); self.mapView.zoomLevel = 10; [self.mapView layoutIfNeeded]; - XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2); + XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2 + scaleBarLastLabelWidthHint); CGRect frame = self.mapView.frame; frame.size = CGSizeMake(frame.size.width/2, frame.size.height); self.mapView.frame = frame; [self.mapView layoutIfNeeded]; - // The bar maximum bar width can only limit the bar's total width. - // Sometimes we should include some space for last label width, the maximum last label width is 30. - // We add this hint value to avoid testing failed when need a extra space for last label. - CGFloat scaleBarLabelWidthHint = 30.0f; + XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2 + scaleBarLastLabelWidthHint); + + self.mapView.scaleBarMaximumWidthRatio = 0.3; - XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width/2 + scaleBarLabelWidthHint); + XCTAssertLessThanOrEqual(scaleBar.intrinsicContentSize.width, self.mapView.frame.size.width * 0.3 + scaleBarLastLabelWidthHint); } @end From d01f69e9238edabf90477c028b899462d7bf66e2 Mon Sep 17 00:00:00 2001 From: m-stephen Date: Tue, 31 Dec 2019 16:32:40 +0800 Subject: [PATCH 16/17] Update CHANGELOG.md --- platform/ios/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index 10989a48c5..46d6857928 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -4,7 +4,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT ## Master -* Add a `maximumWidthRatio` property for customizing maximum width of scale bar. ([#65](https://github.com/mapbox/mapbox-gl-native-ios/pull/65)) +* Added `MGLMapView.maximumWidthRatio` property for customizing the width ratio of map view's scale bar. ([#65](https://github.com/mapbox/mapbox-gl-native-ios/pull/65)) ## 5.6.0 - December 19, 2019 This release includes a known issue where the binary size has increased. [#63](https://github.com/mapbox/mapbox-gl-native-ios/issues/63) From 3ef4a4c30e7d5e388d7645c265121bf59de61ebf Mon Sep 17 00:00:00 2001 From: "M.Stephen" Date: Tue, 11 Feb 2020 22:40:20 +0800 Subject: [PATCH 17/17] add debug log --- platform/ios/src/MGLMapView.mm | 1 + 1 file changed, 1 insertion(+) diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index d17b37c25f..7b5c0cbfcc 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -808,6 +808,7 @@ - (void)setScaleBarMargins:(CGPoint)scaleBarMargins { } - (void)setScaleBarMaximumWidthRatio:(CGFloat)scaleBarMaximumWidthRatio { + MGLLogDebug(@"Setting scale bar maximum width ratio: %f", scaleBarMaximumWidthRatio); _scaleBarMaximumWidthRatio = scaleBarMaximumWidthRatio; if([self.scaleBar isKindOfClass:[MGLScaleBar class]]) { ((MGLScaleBar *)self.scaleBar).maximumWidthRatio = scaleBarMaximumWidthRatio;