UIImageView作为callout的leftCalloutAccessoryView自动切换左上角

时间:2014-09-21 10:13:04

标签: ios objective-c ios8 mkannotation mkannotationview

它在iOS 7上运行良好,但UIImageView的位置在iO8上移动,如下图所示。 rightCalloutAccessoryView位置也会移动到右上角。

有人可以帮忙吗?

干杯。

在iOS 7中

iOS 7

在iOS 8中

iOS8

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)视图 {

if(![view.annotation isKindOfClass:[MKUserLocation class]]) {

    UBAnnotation *selectedAnnotation = view.annotation;

    NSURL * imageUrlAtIndex = [NSURL URLWithString:[[self.objects objectAtIndex:selectedAnnotation.idx] ad_thumbImageUrl]];


    UIImageView * leftCalloutView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 2, 40, 40)];
    [leftCalloutView setImageWithURL:imageUrlAtIndex];
    leftCalloutView.layer.masksToBounds = YES;
    leftCalloutView.layer.cornerRadius = 6;

    view.leftCalloutAccessoryView = leftCalloutView;

}

}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {

MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

if ([annotation isKindOfClass:[MKUserLocation class]]) {
    return nil;
} else {
    annView.image = [UIImage imageNamed:@"pin"];

    annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annView.rightCalloutAccessoryView.tintColor = [UBColor ubGreenColor];
    annView.animatesDrop= NO;
    annView.canShowCallout = YES;

}

return annView;

}

3 个答案:

答案 0 :(得分:6)

我解决了!

所有关于titleLabel的字符串长度。如果字符串长度超过20-25个字符,则左右附件视图会向上移动。

首先,我修剪了标题标签或字幕标签上显示的字符串。之后,连接" ..."作为字符串末尾的字符串。

解决方案有点破解,但它就像魅力一样。

答案 1 :(得分:0)

我做了这样的修复。需要优化代码,但对我来说它是有效的。

- (UIView*)rightCalloutAccessoryView {
    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return [self parentViewForView:rightButton];
}

- (UIView*)parentViewForView:(UIView*)view {
#ifdef __IPHONE_8_0
    CGRect rr = view.frame;
    rr.origin.x = 2.0f;
    rr.origin.y = (64.0f - rr.size.height ) / 2.0f;
    view.frame = rr;
    UIView *result = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rr.size.width + 4.0f, 64.0f)];
    [result addSubview:view];
    return result;
#else
    return view;
#endif
}

答案 2 :(得分:0)

如果你想自定义leftCalloutAccessoryView,你需要在iPhone 4,4S,5,5 +上实现一个你必须优化225.2f(更好的设置是220.0f)的自定义View。

例如:

UIView *_customContentView    = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 225.0f, 38.0f)];
view.leftCalloutAccessoryView = _customContentView;

这是我解决的经历。