首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有自动布局的MKMapView自定义callOutView iOS 6与iOS7

带有自动布局的MKMapView自定义callOutView iOS 6与iOS7
EN

Stack Overflow用户
提问于 2013-10-27 13:10:32
回答 1查看 1.2K关注 0票数 1

嘿伙计们,

如果我在标注视图中使用自动布局,我的应用程序就会在MKMapView 6下崩溃。使用iOS 7,这是很好的工作。

代码语言:javascript
复制
- (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];
}}

控制台显示以下错误:

/SourceCache/UIKit_Sim/UIKit-2380.17/UIView.m:5776 2013-27 13:39:18.519 Layout烈士9825:907 *终止应用程序,原因是:“执行-layoutSubviews之后仍然需要自动布局。MKAnnotationView实现-layoutSubviews需要调用Super.”。

有人能告诉我问题出在哪里吗?CustomCalloutView也适用于自动布局,我不会在其中覆盖layoutSubviews。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-27 21:28:16

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

这也适用于iOS 7。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19618415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档