我有一个 MKMapView,其中有一些路线有时会重叠,并且希望将特定路线放在 View 前面。我正在尝试这个,但似乎不起作用:
[[polylineViewZoom superview] bringSubviewToFront:polylineViewZoom];
polylineViewZoom is a MKPolylineView.
提前致谢!
最佳答案
而不是访问MKPolylineView
的superview
直接使用insertOverlay:atIndex:
MKMapView
提供的方法类。
insertOverlay:atIndex:
方法重新定位已添加到 View /显示层次结构中的 map (使用 addOverlay
或 addOverlays
)的叠加层。我所说的“重新定位”并不是指叠加层的坐标发生变化——只是它的 z 索引控制其与其他叠加层的重叠。
您通过了id<MKOverlay>
反对此方法(不是 MKOverlayView
或 MKOverlayRenderer
)。 map View 在内部负责根据需要操作 View 或图层。
此方法适用于 iOS 6 和 iOS 7。
对于index
参数,值为 0 表示 View 层次结构的“底部”和最高层的覆盖 index
将位于“顶部”或“前面”。
因此,如果您向 map 添加了 5 个叠加层,则 index
范围是从 0 到 4 并设置覆盖的 index
到 4 会将其带到前面。
示例:
//Somewhere earlier, you add the overlay to the map view using
//addOverlay or addOverlays.
//...
//Later, when you want to bring an overlay to the front, get a reference
//to the overlay either from the map view's overlays array
//or you might keep a reference to it in an instance variable.
id<MKOverlay> someOverlay = ...;
//calculate index for the last ("top") overlay...
NSUInteger maxIndex = self.mapView.overlays.count - 1;
[self.mapView insertOverlay:someOverlay atIndex:maxIndex];
请注意,还有一些与更改覆盖层次结构相关的其他方法,您可能会发现它们很有用(例如 exchangeOverlay:withOverlay:
、 insertOverlay:aboveOverlay:
等)。请参阅MKMapView
类引用以获取详细信息。
关于ios - 无法将 MKPolylineView 置于前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23293179/