在我的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];我创建了两个约束,使其全屏显示:
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方法。
我该如何解决这个问题呢?
发布于 2013-01-12 01:24:49
我通过简单地使用内容视图来填充所有内容来解决了这个问题。我没有将工具栏添加到myMapView,而是将其添加到了myContentView。
我很确定MKMapView还没有设置为使用约束。MapView不会调用[super layoutSubviews],这最终会导致[UIView layoutSubviews]。
我设想的另一个可能的解决方案是向MKMapView添加一个类别,该类别将覆盖-layoutSubview以调用[super layoutSubview],但是我不知道如何找出需要添加的[MKMapView layoutSubview]中的其他内容。
https://stackoverflow.com/questions/14263232
复制相似问题