MKMapView自定义callOutView,自动布局iOS 6与iOS7

时间:2013-10-27 13:10:32

标签: ios6 ios7 mkmapview autolayout mkannotationview

Hej Folks,

如果我使用自动布局作为标注视图,我的应用程序会在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。

1 个答案:

答案 0 :(得分:1)

SO Answer指出了我正确的方向。我在MKAnnotationView上创建一个类别,覆盖layoutSubviews方法并调用super方法。

这也适用于iOS 7.