In xcode 9 with iOS 11 - issue with loading of Map tiles on first run

前端 未结 4 2099
孤城傲影
孤城傲影 2021-02-13 12:53

--Updated with new findings -- Tested in both simulator and on device. Maps are not loaded correctly when the app is run from a cold start. Tiles are not being displayed.

<
4条回答
  •  感动是毒
    2021-02-13 13:54

    Had same problem my BROKEN code looked something like:

    class ViewController: UIViewController {
    
        fileprivate var mapView = MKMapView()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Add the mapView to the VC
            mapView.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(mapView)
            mapView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
            mapView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
            mapView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
            mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        }
    }
    

    As it turned out in iOS 11 it does not like having the MKMapView initialize before the view controller has loaded ie viewDidLoad() so FIX was changing to:

    class ViewController: UIViewController {
    
        fileprivate var mapView: CSMapView?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Init instance here.
            mapView = CSMapView()
    
            // Add the mapView to the VC
            mapView?.translatesAutoresizingMaskIntoConstraints = false
            ...
        }
    }
    

    Also tried putting it in init methods but this DID NOT work for me:

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        mapView = MKMapView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        mapView = MKMapView()
    }
    

提交回复
热议问题