指南针在MKMapView中放置或定位?

时间:2016-07-10 03:21:01

标签: swift mkmapview

我正在使用Swift编写一个基于位置的应用程序。

我让我的MapView可以显示指南针,但我想将指南针位置从右上角改为左下角。

是否有专家知道如何移动它?

先谢谢你。

2 个答案:

答案 0 :(得分:7)

您可以将指南针可见性设置为false,然后添加新指南针

mapView.showsCompass = false

let compassBtn = MKCompassButton(mapView:mapView)
compassBtn.frame.origin = CGPoint(x: self.view.frame.maxX - 40, y: 20)
compassBtn.compassVisibility = .adaptive
view.addSubview(compassBtn)

您可以使用自适应可见性,以便新指南针的行为与原始指南针一样。

答案 1 :(得分:5)

您可以尝试更改此内容:

mapView.layoutMargins 

或者继承MKMapView组件:

import MapKit

class MyMapView: MKMapView {    
    override func layoutSubviews() {
        super.layoutSubviews()

        if let compassView = self.subviews.filter({ $0.isKindOfClass(NSClassFromString("MKCompassView")!) }).first {
            compassView.frame = CGRectMake(15, 30, 36, 36)
        }
    }
}
相关问题