MKMapView的ContentInset

12

UIScrollViewcontentInset 属性非常优秀,可以告诉视图哪一部分在屏幕上可见。我有一个被半透明视图覆盖了一部分的 MKMapView。我希望在这个视图下方能够看到地图。我需要在地图上显示几个注释,并且想使用 -setRegion:animated: 缩放到这些注释,但是地图视图不考虑它被部分遮盖,因此我的某些注释将被透明视图遮挡。

enter image description here

是否有办法告诉地图计算并使用 contentInset 呢?


更新:我已经尝试了以下方法:

- (MKMapRect)mapRectForAnnotations
{
    if (self.trafik) {
        MKMapPoint point = MKMapPointForCoordinate(self.trafik.coordinate);
        MKMapPoint deltaPoint;

        if (self.map.userLocation &&
            self.map.userLocation.coordinate.longitude != 0) {
            MKCoordinateSpan delta = MKCoordinateSpanMake(fabsf(self.trafik.coordinate.latitude-self.map.userLocation.coordinate.latitude),
                                                          fabsf(self.trafik.coordinate.longitude-self.map.userLocation.coordinate.longitude));
            deltaPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(delta.latitudeDelta, delta.longitudeDelta));
        } else {
            deltaPoint = MKMapPointForCoordinate(CLLocationCoordinate2DMake(0.01, 0.01));
        }

        return MKMapRectMake(point.x, point.y, deltaPoint.x, deltaPoint.y);
    } else {
        return MKMapRectNull;
    }
}

是的,setVisibleMapRect:edgePadding:animated: 方法应该能帮到您。计算适合注释的 MKMapRect,然后根据需要添加边缘填充。请参见 https://dev59.com/X2445IYBdhLWcg3w0ddD#7141612 以了解如何计算 MKMapRect 的示例。 - user467105
我已经更新了我的问题,包括我尝试计算这个MKMapRect的方法,但是它一定有错误,因为我总是得到一个完整的世界地图。 :( - gklka
2个回答

12

使用 UIViewslayoutMargins 属性。

例如,这将强制当前用户位置的图钉上移 50pts

mapView.layoutMargins = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 100.0, right: 0.0)

3

您可以按照以下方法操作,但这可能会影响使用bottomLayoutGuide的其他视图。您需要进行测试以确定。

在包含地图的UIViewController中重写bottomLayoutGuide并返回一个类似于MyLayoutGuide对象的内容:

@interface MyLayoutGuide : NSObject <UILayoutSupport>
@property (nonatomic) CGFloat length;
-(id)initWithLength:(CGFloat)length;
@end

@implementation MyLayoutGuide
@synthesize length = _length;
@synthesize topAnchor = _topAnchor;
@synthesize bottomAnchor = _bottomAnchor;
@synthesize heightAnchor = _heightAnchor;

- (id)initWithLength:(CGFloat)length
{
    if (self = [super init]) {
        _length = length;
    }
    return self;
}

@end

通过将 bottomLayoutGuideMKMapView 的内边距调整为 50 点:

- (id)bottomLayoutGuide
{
    CGFloat bottomLayoutGuideLength = 50.f;

    return [[MyLayoutGuide alloc] initWithLength:bottomLayoutGuideLength];
}

如果底部的时间表更改了大小,您可以通过在 MKMapView 上调用 setNeedsLayout 来强制重新计算此“插入”。我们在MKMapView的子类中创建了一个帮助程序,可以从父级 UIViewController 中调用:

- (void)updateBottomLayoutGuides
{
    // this method ends up calling -(id)bottomLayoutGuide on its UIViewController
    // and thus updating where the Legal link on the map should be.
    [self.mapView setNeedsLayout];
}

以下答案改编自此回答


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接