[英]iOS - MKMapView showAnnotations:animated: with padding?
我希望能够缩放我的MKMapView以适应它的注释。 我已经设法使用iOS7的showAnnotations
方法。 但我还想从地图视图边框添加一些填充或插入。 这是因为我有一个半透明的视图覆盖了我的地图的顶部,我不希望注释成为这个视图背后的地方。 我试过这个:
[self.mapView showAnnotations:annotations animated:YES];
[self.mapView setVisibleMapRect:self.mapView.visibleMapRect edgePadding:UIEdgeInsetsMake(100, 20, 10, 10) animated:NO];
但它没有像我希望的那样工作。 关于如何以不同方式做到这一点的任何想法?
你也可以简单地使用
[self.mapView showAnnotations:annotations animated:YES];
self.mapView.camera.altitude *= 1.4;
缩小一点。 对我来说效果很好。
从iOS8开始, MKMapView
具有layoutMargin
属性。 设置此项时, centerRegion:
, showAnnotations:
以及尝试在地图视图中拟合矩形的所有方法都将考虑上述布局边距。
如果您的半透明视图高度为40点并且附加到地图视图的顶部,则设置mapView.layoutMargin = UIEdgeInsetMake(40, 0, 0, 0)
将完成魔术。
如果以iOS7为目标,地图视图将使用其包含控制器的顶部和底部布局指南来偏移其内容。 因此,您可以覆盖控制器的topLayoutGuide
方法以返回所需的长度。
class ViewController: UIViewController {
override var topLayoutGuide: UILayoutSupport {
return MapLayoutGuide(length: 40)
}
}
class MapLayoutGuide: NSObject, UILayoutSupport {
var length: CGFloat
init(length: CGFloat) {
self.length = length
super.init()
}
@available(iOS 9.0, *)
var bottomAnchor: NSLayoutYAxisAnchor {
return NSLayoutYAxisAnchor()
}
@available(iOS 9.0, *)
var topAnchor: NSLayoutYAxisAnchor {
return NSLayoutYAxisAnchor()
}
@available(iOS 9.0, *)
var heightAnchor: NSLayoutDimension {
return NSLayoutDimension()
}
}
你正在以正确的方式做到这一点。 尝试更改填充,你会看到差异。
否则,代码中必须有其他内容阻止更改视图
编辑:我完全错了。 尝试这个:
创建一个实例变量
BOOL _mapNeedsPadding;
并将其初始化为NO;
然后将mapView委托设置为self并将a添加到类标题中
然后将其添加到您的班级
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
if(_mapNeedsPadding){
_mapNeedsPadding = NO;
[self.mapView setVisibleMapRect:self.mapView.visibleMapRect edgePadding:UIEdgeInsetsMake(100, 20, 10, 10) animated:YES];
}
}
最后像这样调用showAnnotations函数:
_mapNeedsPadding = YES;
[self.mapView showAnnotations:annotations animated:YES];
showAnnimation将触发regionDidChangeAnimated函数。 更改visibleMapRect后需要将_mapNeedsPadding设置为NO,因为此函数(setVisibleMapRect:self)也将触发regionDidChangeAnimated。
希望这可以帮助 !
另一种方案:
extension MKMapView
{
func fitAllMarkers(inset: UIEdgeInsets? = nil) {
self.showAnnotations(self.annotations, animated: false)
OperationQueue.main.addOperation {
self.setVisibleMapRect(self.visibleMapRect, edgePadding: inset ?? UIEdgeInsets.zero, animated: true)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.