In my viewDidLoad method, I have code to create a MKMapView, some constraints, and a UIToolbar:
I have a 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 it full screen:
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];
Works great. But when I try to add anything 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 throws 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's implementation of -layoutSubviews needs to call super.'
From my understanding, since I am adding a new view to the map view, the map view needs to re-calculate all the constraints in it? Basically a drawRect method for constraints.
How can I fix this?
I solved the issue by simply using a content view to stuff everything into. Instead of adding the tool bar to myMapView, i add it to myContentView.
I’m pretty sure MKMapView isn’t set up to use constraints yet. MapView won’t call
[super layoutSubviews]which would eventually lead to[UIView layoutSubviews].Another possible solution i’d imagine is to add a category to MKMapView that would override
-layoutSubviewto call[super layoutSubview], but i don’t know how to find out what else is in the[MKMapView layoutSubview]that i would need to add.