1

I'd like to change the position of the compass like below:

img

This is what I tried so far but it doesn't seems to be the right way - Yeah, it is not working.

if let compassView = self.view.subviews.filter({ ($0.subviews.first?.isKind(of: NSClassFromString("MKCompassView")!))!}).first {
    // set compassView position
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jonas0000
  • 1,085
  • 1
  • 12
  • 36

1 Answers1

0

You can try contains in IOS < 11

if let compassBtn = (mapView.subviews.filter { String(describing:$0).contains("MKCompassView") }.first) {
  print(compassBtn)
 }

Also if your deployment target >= IOS 11 you can use MKCompassButton

 let compassBtn = MKCompassButton(mapView:mapView)
 compassBtn.frame.origin = CGPoint(x: 25, y: 25)
 compassBtn.compassVisibility = .visible
 view.addSubview(compassBtn)

Edit:

if let compassView = (mapView.subviews.filter({ $0 is NSClassFromString("MKCompassView")! }).first) {  }
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • It looks like the` first code ` is not working either. The second code looks quiet well but I'm still getting `two compass Views` instead of one. How can I fix this? Edit: `Your first code will not print anything out, it's not working however I' trying it.` – Jonas0000 Feb 09 '18 at 19:01
  • First: Thanks for your response. :-) But tbh your edit is still not working. I'm getting this error:` Consecutive statements on a line must be separated by ';' `. Edit: I'm using Swift 4 – Jonas0000 Feb 09 '18 at 19:36
  • Are you still there @Sh_Khan?? – Jonas0000 Feb 09 '18 at 20:22
  • I may try it later and gives you feedback – Shehata Gamal Feb 09 '18 at 20:27