NSLayoutConstraint and MKMapView

In my viewDidLoad method, I have code to create MKMapView, some limitations and UIToolbar:

I have MKMapView:

MKMapView *mapView = [[MKMapView alloc] init]; [mapView setTranslatesAutoresizingMaskIntoConstraints:NO]; mapView.userInteractionEnabled = TRUE; mapView.showsUserLocation = TRUE; mapView.mapType = MKMapTypeHybrid; mapView.delegate = self; [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES]; [self.view addSubview:mapView]; 

I create 2 constraints to make them fullscreen:

 NSMutableArray *constraints = [NSMutableArray array]; [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mapView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(mapView)]]; [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[mapView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(mapView)]]; [self.view addConstraints:constraints]; 

It works great. But when I try to add something to the map view:

  UIToolbar *topBar = [[UIToolbar alloc ]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; [topBar setTranslatesAutoresizingMaskIntoConstraints:NO]; topBar.barStyle = UIBarStyleBlackTranslucent; [mapView addSubview:topBar]; 

it gives an error:

 *** Assertion failure in -[MKMapView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2372/UIView.m:5776 2013-01-10 10:24:17.503 Landscout 2[2001:14003] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. MKMapView implementation of -layoutSubviews needs to call super.' 

From my point of view, since I am adding a new view to the map view, is it necessary to recalculate all restrictions in it to display the map? Basically, the drawRect method for constraints.

How can i fix this?

+4
source share
1 answer

I solved the problem by simply using the content view to paste it all. Instead of adding a toolbar to myMapView, I add it to myContentView.

I am sure MKMapView is not yet configured to use restrictions. MapView will not call [super layoutSubviews] , which will ultimately result in [UIView layoutSubviews] .

Another possible solution I can imagine is to add a category to MKMapView that will override -layoutSubview to call [super layoutSubview] , but I don’t know how to find out what else is in [MKMapView layoutSubview] that I would need to add .

+6
source

All Articles