如何显示MapKit指南针? [英] How to show MapKit compass?
问题描述
当前,只有用户应用旋转手势时,才会显示指南针.否则,指南针将被隐藏.
Currently the compass only get's shown if a User applies a rotate-gesture. Otherwise the compass is hidden.
但是,如果我的以下两个愿望都能实现,那就太好了!
But it would be nice if my two wishes below were fulfilled!
- 是否可以始终显示指南针?
- 如何使用Swift显示/隐藏指南针视图?
推荐答案
You can do this quite easily in iOS 11 by using the new MKCompassButton
class.
您需要创建 MKCompassButton
的实例并将其添加到地图视图中.然后,您可以将其 compassVisibility
属性设置为以下其中一项:
You need to create an instance of MKCompassButton
and add it to your map view. You can then set its compassVisibility
property to one of:
-
.visible
-始终可见 -
.从不
-从不可见 -
.adaptive
-仅当地图从北/上方向旋转离开时,指南针才可见.
.visible
- always visible.never
- never visible.adaptive
- the compasss is only visible if the map is rotated away from a North/up orientation.
如果在属性中保留对指南针的引用,则可以根据需要更改其可见性:
If you keep a reference to the compass in a property you can change its visibility as you need:
mapview.showsCompass = false // Hide built-in compass
compassButton = MKCompassButton(mapView: mapview) // Make a new compass
compassButton.compassVisibility = .visible // Make it visible
mapview.addSubview(compassButton) // Add it to the view
// Position it as required
compassButton.translatesAutoresizingMaskIntoConstraints = false
compassButton.trailingAnchor.constraint(equalTo: mapview.trailingAnchor, constant: -12).isActive = true
compassButton.topAnchor.constraint(equalTo: mapview.topAnchor, constant: 12).isActive = true
不幸的是,对于以前的iOS版本,没有简单的解决方案.我已经看到一些建议,其中涉及浏览地图视图的子视图以尝试并找到指南针视图,但结果似乎参差不齐.
Unfortunately, for prior versions of iOS there is no simple solution. I have seen suggestions that involve looking through the map view's subviews to try and find the compass view but there seems to be mixed results.
这篇关于如何显示MapKit指南针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!