【问题标题】:Layout relation between MKAnnotationView and its rightCalloutAccessoryView has changed in iOS7MKAnnotationView 与其 rightCalloutAccessoryView 的布局关系在 iOS7 中发生了变化
【发布时间】:2013-09-29 17:49:52
【问题描述】:

UIButton 设置为MKPinAnnotationViewrightCalloutAccessoryView,并触发btnClicked: 方法。在 iOS6 中,MKAnnotationView 与其rightCalloutAccessoryView 的布局关系可以很容易地找到MKPinAnnotationViewMKPinAnnotationView *pin = (MKPinAnnotationView *)[[button superview] superview];。但是在iOS7中,布局关系已经不存在了。

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation{
...

MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:tripStartAddrAnnotationIdentifier];
    if (!pinView)
    {            
        MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc]
                                              initWithAnnotation:annotation reuseIdentifier:tripStartAddrAnnotationIdentifier];
        customPinView.animatesDrop = NO;
        customPinView.canShowCallout = YES;

        UIButton* rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
        [rightButton setImage:[UIImage imageNamed:@"disclosureUpMap.png"] forState:UIControlStateNormal];
        [rightButton setImage:[UIImage imageNamed:@"disclosureDownMap.png"] forState:UIControlStateNormal];

        [rightButton addTarget:self
                        action:@selector(btnClicked:)
              forControlEvents:UIControlEventTouchUpInside];
        customPinView.rightCalloutAccessoryView = rightButton;

        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
...
}


-(void)btnClicked:(id)sender{
    NSLog(@"superview 1 :%@",[sender superview]);
    NSLog(@"superview 2 :%@",[[sender superview] superview]);
    NSLog(@"superview 3 :%@",[[[sender superview] superview] superview]);
    NSLog(@"superview 4 :%@",[[[[sender superview] superview] superview] superview]);
    MKPinAnnotationView *pin = (MKPinAnnotationView *)[[sender superview] superview];
    // the information contains in pin.annotation is necessary for the pushed view controller
...    
}

iOS6 的日志:

2013-09-30 01:21:11.851 MyApp[6113:c07] superview 1 :<UICalloutView: 0x1e3df250; frame = (-246 -60; 320 70); clipsToBounds = YES; layer = <CALayer: 0x1e3e0140>>
2013-09-30 01:21:11.851 MyApp[6113:c07] superview 2 :<MKPinAnnotationView: 0x1b649510; frame = (526 223; 32 39); layer = <MKLayer: 0x1b649570>> visible:1 +33.19910200, +120.45132100
2013-09-30 01:21:11.852 MyApp[6113:c07] superview 3 :<MKAnnotationContainerView: 0xb68b380; frame = (0 0; 640 640); autoresizesSubviews = NO; layer = <CALayer: 0xb68b410>>
2013-09-30 01:21:11.852 MyApp[6113:c07] superview 4 :<MKScrollContainerView: 0xb68c8f0; frame = (-280 -72.5; 640 640); autoresizesSubviews = NO; layer = <CALayer: 0xb68c980>>

iOS7 的日志:

2013-09-30 01:44:22.835 MyApp[6226:a0b] superview 1 :<_MKSmallCalloutContainerView: 0xdbe5120; frame = (0 0; 222 44); clipsToBounds = YES; layer = <CALayer: 0xdbe1f30>>
2013-09-30 01:44:22.836 MyApp[6226:a0b] superview 2 :<MKSmallCalloutView: 0x1c53a380; frame = (0 0; 222 57); layer = <CALayer: 0x1c546440>>
2013-09-30 01:44:22.837 MyApp[6226:a0b] superview 3 :<UIView: 0xdb14770; frame = (0 0; 222 57); clipsToBounds = YES; layer = <CALayer: 0xdb147d0>>
2013-09-30 01:44:22.837 MyApp[6226:a0b] superview 4 :<_UIPopoverView: 0xdbfa4c0; frame = (-27 -57; 222 57); layer = <CALayer: 0xdbfa5a0>>

任何帮助将不胜感激。

【问题讨论】:

    标签: ios ios7 mkannotationview


    【解决方案1】:

    这是使用地图视图自己的委托方法 calloutAccessoryControlTapped 而不是自定义方法的重要原因。

    即使 superview 方法在 iOS 6 或更早版本中有效,依赖特定的视图层次结构通常也是一个坏主意。

    委托方法方便地提供对注释视图的引用,您可以从中通过view.annotation 获取注释(无需猜测或希望或假设)。

    移除 addTarget 并将自定义方法替换为委托方法。

    如果您必须使用自定义方法而不是委托方法,则从地图视图的selectedAnnotations 属性中获取选定的注释而不是寻找超级视图更可靠。

    猜你喜欢
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多