Compass placing or position in a MKMapView? [duplicate]
Asked Answered
G

2

6

I am coding one Location based App by Swift.

I make my MapView can show Compass, but I would like to change the compass location from the right-up corner to the left-down corner.

Does any expert know how to move it?

Thank you in advanced.

Gaillard answered 10/7, 2016 at 3:21 Comment(1)
I have no idea why this question was closed in favor of a newer question, particularly as this question has a roughly correct answer . anyway I put in the full correct modern answer on the other question, https://mcmap.net/q/1630405/-how-to-reposition-compass-of-mkmapviewHadria
E
8

You can try to change this:

mapView.layoutMargins 

Or subclass the MKMapView component:

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)
        }
    }
}
Engels answered 9/1, 2017 at 11:43 Comment(6)
Can you verify if the second method still works? I cannot find any view with class MKCompassView in the subviews of my instance of MKMapView. The compass is enabled and showing but inaccessible.Mcpherson
@Ben-Ong The MKCompassView is definitely still a subview of MKMapView, though it's only in the hierarchy when the map is rotated and the compass is shown. I just checked against iOS 9.1 in the simulator, using XCode's "Debug View Hierarchy".Brainsick
Can someone verify this approach works and passes Apple's review? There's no doc on MKCompassView, and MKCompassButton is only available publicly starting iOS 11. This screams private API access to me.Carcanet
Confirmed that: mapView.layoutMargins = view.safeAreaInsets worked best for me.Zumstein
This also works fine: mapView.layoutMargins = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 8)Dichogamy
Absolutely does not work, for years now. fully explained here https://mcmap.net/q/1630405/-how-to-reposition-compass-of-mkmapviewHadria
J
28

You can do this by setting the compass visibility to false and then adding a new compass

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)

You can use adaptive visibility so your new compass will behave like the original one.

Josefina answered 7/4, 2018 at 12:11 Comment(1)
This is only available from iOS11, is there a way for iOS 9 and 10 as well?Cytogenesis
E
8

You can try to change this:

mapView.layoutMargins 

Or subclass the MKMapView component:

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)
        }
    }
}
Engels answered 9/1, 2017 at 11:43 Comment(6)
Can you verify if the second method still works? I cannot find any view with class MKCompassView in the subviews of my instance of MKMapView. The compass is enabled and showing but inaccessible.Mcpherson
@Ben-Ong The MKCompassView is definitely still a subview of MKMapView, though it's only in the hierarchy when the map is rotated and the compass is shown. I just checked against iOS 9.1 in the simulator, using XCode's "Debug View Hierarchy".Brainsick
Can someone verify this approach works and passes Apple's review? There's no doc on MKCompassView, and MKCompassButton is only available publicly starting iOS 11. This screams private API access to me.Carcanet
Confirmed that: mapView.layoutMargins = view.safeAreaInsets worked best for me.Zumstein
This also works fine: mapView.layoutMargins = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 8)Dichogamy
Absolutely does not work, for years now. fully explained here https://mcmap.net/q/1630405/-how-to-reposition-compass-of-mkmapviewHadria

© 2022 - 2024 — McMap. All rights reserved.