赫吉乡亲们,
如果我对标注视图使用自动布局,我的应用程序会在 iOS 6 下的 MKMapView 上崩溃。使用 iOS 7,这可以正常工作。
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
CustomMapAnnotation *annotation = (CustomMapAnnotation *)view.annotation;
if([annotation isKindOfClass:[CustomMapAnnotation class]]) {
CustomMapCalloutView *calloutView = [CustomMapCalloutView new];
calloutView.translatesAutoresizingMaskIntoConstraints = NO;
calloutView.titleLabel.text = annotation.titleText;
calloutView.subTitleLabel.text = annotation.subTitleText;
calloutView.distanceTextLabel.text = annotation.distanceText;
[view addSubview:calloutView];
NSDictionary *viewsDictionary = @{@"callOutView": calloutView};
NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[callOutView(150)]" options:0 metrics:nil views:viewsDictionary];
NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[callOutView(50)]" options:0 metrics:nil views:viewsDictionary];
NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:calloutView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:calloutView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
[view addConstraint:xConstraint];
[view addConstraint:yConstraint];
[view addConstraints:hConstraints];
[view addConstraints:vConstraints];
}}
控制台显示以下错误:
* -[MKAnnotationView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2380.17/UIView.m:5776 2013-10-27 13:39:18.519 PartySmarty[9825:907] 中的断言失败 *由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序', 原因:'执行 -layoutSubviews 后仍需要自动布局。MKAnnotationView 的 -layoutSubviews 实现需要调用 super.'
有人可以给我一个提示问题出在哪里吗?CustomCalloutView 也适用于自动布局,我不会覆盖其中的 layoutSubviews。