2017-11-13 2 views
1

Ich mache eine iOS-Anwendung. In Xcode 9.1 erstelle ich eine MKMapView vonMKMapView Maßstab wird nicht angezeigt

let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)) 
mapView.isUserInteractionEnabled = false 
mapView.mapType = .satellite 
mapView.showsCompass = false 
mapView.showsScale = true 
view.addSubview(mapView) 

aber wenn ich es im Simulator ausgeführt wird, die Waage nicht gezeigt und ich drei Meldungen im Protokoll:

kann nicht Einsatz Kompass von Kanten 9

kann nicht inset Skala von Kante 9

kann nicht legal Zuschreibung von der Ecke nach innen versetzt 4

Der Kompass wird nicht angezeigt (wie erwartet), aber es wird nicht angezeigt, wenn ich auch mapView.showsCompass zu true ändern. Der Link "Rechtlich" wird jedoch angezeigt. Was fehlt mir hier? Ich vermute, dass es etwas mit den neuen sicheren Bereichen von iOS 11 zu tun hat, aber ich sehe nicht, wie wichtig das für eine Ansicht ist, die den gesamten Bildschirm abdecken soll.

+0

In iOS 11 die Der Kompass wird nur angezeigt, wenn die Karte in aw gedreht ist ay von Norden und die Skala wird beim Zoomen angezeigt. Ich empfehle die WWDC-Sitzung zu den Neuerungen in MapKit. Ich denke du kannst diese Nachrichten ignorieren. – Paulw11

+0

Die WWDC-Sitzung erklärt, wie man eine Kompass-Taste hinzufügt, damit immer ein Kompass angezeigt wird – Paulw11

+0

Ich werde die WWDC-Sitzung sehen, aber die Frage war, die Waage zu zeigen. Der Code setzt den Kompass so, dass er nicht angezeigt wird. Das ist also kein Problem. –

Antwort

0

hatte das gleiche Problem mit dem heutigen Maßstab. Ich möchte, dass diese Skala immer sichtbar ist. Kostet mich mehrere Stunden um es zu lösen. Also füge ich den Code hier hinzu, nur für den Fall, dass jemand auf das gleiche Problem stößt.

Got einige Hinweise:

aus diesem Thread: Use Safe Area Layout programmatically

und diese Seite: Pain Free Constraints with Layout Anchors

Glückliche Codierung ...

Hardy

// "self.MapOnScreen" refers to the map currently displayed 

// check if we have to deal with the scale 
if #available(iOS 11.0, *) { 

    // as we will change the UI, ensure it's on main thread 
    DispatchQueue.main.async(execute: { 

     // switch OFF the standard scale (otherwise both will be visible when zoom in/out) 
     self.MapOnScreen.showsScale = false 

     // build the view 
     let scale = MKScaleView(mapView: self.MapOnScreen) 

     // we want to use autolayout 
     scale.translatesAutoresizingMaskIntoConstraints = false 

     // scale should be visible all the time 
     scale.scaleVisibility = .visible // always visible 

     // add it to the map 
     self.MapOnScreen.addSubview(scale) 

     // get the current safe area of the map 
     let guide = self.MapOnScreen.safeAreaLayoutGuide 

     // Activate this array of constraints, which at the time removes leftovers if any 
     NSLayoutConstraint.activate(
      [ 
       // LEFT (I do not want a change if right-to-left language) margin with an offset to safe area 
       // alternative would be ".leadingAnchor", which switches to the right margin, if right-to-left language is used   
       scale.leftAnchor.constraint(equalTo: guide.leftAnchor, constant: 16.0), 

       // right edge will be the middle of the map 
       scale.rightAnchor.constraint(equalTo: guide.centerXAnchor), 

       // top margin is the top safe area 
       scale.topAnchor.constraint(equalTo: guide.topAnchor), 

       // view will be 20 points high 
       scale.heightAnchor.constraint(equalToConstant: 20.0) 
      ] 
     ) 
    }) 
} 
Verwandte Themen