MKMapView不呈现(仅当来自背景时)

时间:2018-10-02 16:38:17

标签: ios swift mapkit mkmapview

我正在创建一个没有情节提要的MKMapView。那是我的ViewController类(setupLayout被调用viewDidLoad)和我的MapViewController类:

// MARK: - View Controller Lifecycle
extension ViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupInterface()
    }

}

// MARK: - Interface
extension ViewController: Interface {

    func setupInterface() {
        setupDesign()
        setupStrings()
        setupBindings()
        setupLayout()
    }
}

MapViewController类:

class MapViewController: ViewController {

    internal var cars: [Car]?

    // MARK: Interface Properties
    private var mapView = MKMapView(frame: .zero)

    // MARK: Internal Functions
    internal override func updateCars(cars: [Car]) {
        self.cars = cars
        placeCars()
    }
}

// MARK: - Interface
extension MapViewController {

    // MARK: Strings
    override func setupStrings() {
        super.setupStrings()

        title = Strings.mapViewTitle.localized
    }

    // MARK: Bindings
    override func setupBindings() {
        super.setupBindings()

        mapView.delegate = self
        mapView.register(CarAnnotationView.self, forAnnotationViewWithReuseIdentifier: CarAnnotationView.reuseIdentifier)
        mapView.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: Constants.StartCoordinates.latitude, longitude: Constants.StartCoordinates.longitude), span: MKCoordinateSpan(latitudeDelta: Constants.StartCoordinates.latitudeDelta, longitudeDelta: Constants.StartCoordinates.longitudeDelta)), animated: true)

    }

    // MARK: Layout
    override func setupLayout() {
        super.setupLayout()

        view.addSubview(mapView)
        mapView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

}

未调用委托方法,并且地图无法正确呈现:

enter image description here

如果我再次进入背景和前景,则地图将按预期工作。有什么想法可以解决问题吗?

3 个答案:

答案 0 :(得分:1)

问题在于初始化:它需要在viewDidLoad上发生,而不是在Controller的顶层。

class MapViewController: ViewController {

    private var mapView: MKMapView?

// ...

override func viewDidLoad() {
    super.viewDidLoad()

    setupMapView()
}

func setupMapView() {
    mapView = MKMapView(frame: .zero)
    mapView?.delegate = self
    mapView?.register(CarAnnotationView.self, forAnnotationViewWithReuseIdentifier: CarAnnotationView.reuseIdentifier)
    mapView?.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: Constants.StartCoordinates.latitude, longitude: Constants.StartCoordinates.longitude), span: MKCoordinateSpan(latitudeDelta: Constants.StartCoordinates.latitudeDelta, longitudeDelta: Constants.StartCoordinates.longitudeDelta)), animated: true)

    guard let mapView = mapView else { return }
    view.addSubview(mapView)
    mapView.snp.remakeConstraints { make in
        make.edges.equalToSuperview()
    }
}

答案 1 :(得分:0)

如何用NavigationView navigationView = findViewById(R.id.nav_view); navigationView.bringToFront(); 方法调用makeConstraints

updateViewConstraints

答案 2 :(得分:0)

viewDidLoad上初始化或设置地图不足以解决我的问题:我最终不得不在viewWillAppear上进行设置。