4

私の 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 メソッドです。

どうすればこれを修正できますか?

4

1 に答える 1

6

コンテンツビューを使用してすべてを詰め込むだけで問題を解決しました。ツールバーを myMapView に追加する代わりに、myContentView に追加します。

MKMapViewがまだ制約を使用するように設定されていないと確信しています。MapView は呼び出さ[super layoutSubviews]れず、最終的には[UIView layoutSubviews].

私が想像する別の可能な解決策は、-layoutSubviewcallをオーバーライドする MKMapView にカテゴリを追加することですが、追加する必要[super layoutSubview]がある他に何があるかを見つける方法がわかりません[MKMapView layoutSubview]

于 2013-01-11T17:24:48.790 に答える