Merge pull request #1357 from mapbox/1ec5-view-controller

Automatically find view controller for layout guides
This commit is contained in:
Minh Nguyễn
2015-04-27 22:28:06 -07:00
4 changed files with 23 additions and 26 deletions

View File

@@ -46,11 +46,6 @@ IB_DESIGNABLE
/** @name Managing Constraints */
/** A view controller whose top and bottom layout guides to use for proper setup of constraints in the map view internals.
*
* Certain components of the map view, such as the heading compass and the data attribution button, need to be aware of the view controller layout in order to avoid positioning content under a top navigation bar or a bottom toolbar. */
@property (nonatomic, weak) IBOutlet UIViewController *viewControllerForLayoutGuides;
#pragma mark - Accessing Map Properties
/** @name Accessing Map Properties */

View File

@@ -67,7 +67,6 @@ mbgl::Settings_NSUserDefaults *settings = nullptr;
self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds accessToken:accessToken];
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mapView.viewControllerForLayoutGuides = self;
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
[self.view addSubview:self.mapView];

View File

@@ -323,8 +323,6 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
[container addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleCompassTapGesture:)]];
[self addSubview:container];
self.viewControllerForLayoutGuides = nil;
// setup interaction
//
_pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
@@ -476,15 +474,22 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
[self setNeedsUpdateConstraints];
}
- (void)setViewControllerForLayoutGuides:(UIViewController *)viewController
- (UIViewController *)viewControllerForLayoutGuides
{
_viewControllerForLayoutGuides = viewController;
[self.compass.superview removeConstraints:self.compass.superview.constraints];
[self.logoBug removeConstraints:self.logoBug.constraints];
[self.attributionButton removeConstraints:self.attributionButton.constraints];
[self setNeedsUpdateConstraints];
// Per -[UIResponder nextResponder] documentation, a UIView’s next responder
// is its managing UIViewController if applicable, or otherwise its
// superview. UIWindow’s next responder is UIApplication, which has no next
// responder.
UIResponder *laterResponder = self;
while ([laterResponder isKindOfClass:[UIView class]])
{
laterResponder = laterResponder.nextResponder;
}
if ([laterResponder isKindOfClass:[UIViewController class]])
{
return (UIViewController *)laterResponder;
}
return nil;
}
- (void)updateConstraints
@@ -493,21 +498,20 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
// 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.
//
UIView *constraintParentView = (self.viewControllerForLayoutGuides.view ?
self.viewControllerForLayoutGuides.view :
self);
UIViewController *viewController = self.viewControllerForLayoutGuides;
UIView *constraintParentView = (viewController.view ? viewController.view : self);
// compass
//
UIView *compassContainer = self.compass.superview;
if (self.viewControllerForLayoutGuides)
if (viewController)
{
[constraintParentView addConstraint:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.viewControllerForLayoutGuides.topLayoutGuide
toItem:viewController.topLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1
constant:5]];
@@ -550,10 +554,10 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
// logo bug
//
if (self.viewControllerForLayoutGuides)
if (viewController)
{
[constraintParentView addConstraint:
[NSLayoutConstraint constraintWithItem:self.viewControllerForLayoutGuides.bottomLayoutGuide
[NSLayoutConstraint constraintWithItem:viewController.bottomLayoutGuide
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.logoBug
@@ -581,10 +585,10 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
// attribution button
//
if (self.viewControllerForLayoutGuides)
if (viewController)
{
[constraintParentView addConstraint:
[NSLayoutConstraint constraintWithItem:self.viewControllerForLayoutGuides.bottomLayoutGuide
[NSLayoutConstraint constraintWithItem:viewController.bottomLayoutGuide
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.attributionButton

View File

@@ -12,7 +12,6 @@
_mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds
accessToken:@"pk.eyJ1IjoianVzdGluIiwiYSI6Ik9RX3RRQzAifQ.dmOg_BAp1ywuDZMM7YsXRg"];
_mapView.viewControllerForLayoutGuides = self;
_mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_mapView];