在我的 viewDidLoad 方法中,我有创建 MKMapView、一些约束和 UIToolbar 的代码:
我有一个 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];
我创建了 2 个约束以使其全屏显示:
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];
效果很好。但是当我尝试向地图视图添加任何内容时:
UIToolbar *topBar = [[UIToolbar alloc ]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[topBar setTranslatesAutoresizingMaskIntoConstraints:NO];
topBar.barStyle = UIBarStyleBlackTranslucent;
[mapView addSubview:topBar];
它抛出一个错误:
*** 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.'
据我了解,既然我在地图视图中添加了一个新视图,那么地图视图需要重新计算其中的所有约束吗?基本上是一个用于约束的 drawRect 方法。
我怎样才能解决这个问题?