WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

MKMapView not showing up in Programmatic UI - Could someone shed some light on what I'm doing wrong?

Forums > iOS

Hello, I am attempting to create my first project with a 100% programmatic UI. This project contains an MKMapView to show restaurants near the current user. The issue that I'm having, however, is that the MKMapView isn't showing up, even with the constraints that I've applied. I tried constraining a MKMapView in Storyboard and it shows up just fine. Did I forget to include something that the storyboard does for you? Did a do the constraints properly? Hopefully someone can shed some light on this. Thanks in advance.

My code is:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController {
    var mapView: MKMapView!
    var viewLocationsButton: UIButton!

    override func loadView() {
        self.view = UIView()
        view.backgroundColor = .systemBackground

        viewLocationsButton = UIButton()
        viewLocationsButton.translatesAutoresizingMaskIntoConstraints = false
        viewLocationsButton.backgroundColor = UIColor(red: 0, green: 0, blue: 0.75, alpha: 1)
        viewLocationsButton.tintColor = .white
        viewLocationsButton.setImage(UIImage(systemName: "mappin.and.ellipse"), for: .normal)
        viewLocationsButton.layer.borderWidth = 1
        viewLocationsButton.layer.borderColor = UIColor(red: 0, green: 0, blue: 0.75, alpha: 1).cgColor
        viewLocationsButton.layer.cornerRadius = 15
        viewLocationsButton.setTitle("Show Locations", for: .normal)

        mapView = MKMapView()
        mapView.translatesAutoresizingMaskIntoConstraints = false
        mapView.mapType = .standard
        mapView.centerCoordinate = CLLocationCoordinate2D(latitude: 37.0902, longitude: 95.7129)

        #if os(macOS)
        mapView.showsZoomControls = true
        #endif

        mapView.showsCompass = true
        mapView.showsScale = true
        mapView.isZoomEnabled = true

        view.addSubview(mapView)
        view.addSubview(viewLocationsButton)

        setupConstraints()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func setupConstraints() {
        NSLayoutConstraint.activate([
            viewLocationsButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20),
            viewLocationsButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20),
            viewLocationsButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20),
            viewLocationsButton.heightAnchor.constraint(equalToConstant: 50),

            mapView.topAnchor.constraint(equalTo: view.topAnchor),
            mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            mapView.trailingAnchor.constraint(equalTo: view.leadingAnchor),
            mapView.bottomAnchor.constraint(equalTo: viewLocationsButton.topAnchor, constant: -20)
        ])
    }
}

1      

Skimming the code, you have both the mapView.leadingAnchor and mapView.trailingAnchor set as being equalTo: view.leadingAnchor.

2      

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.