在我viewDidLoad方法中,我有一些代碼來創建的MKMapView,一些制約因素和UIToolbar:NSLayoutConstraint和的MKMapView
我有一個的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方法。
我該如何解決這個問題?
你在哪種方法創建'UIToolbar * topBar'並將其添加爲子視圖? –
代碼全部在viewDidLoad方法內。 – Padin215