Why wont UIView display a second time?

I’m trying to figure out why my newDogView will only present once. This app takes a dog photo, stores it in a struct with some other info, and plots that dog on a map view as an annotation. When you press the newDogButton, a UIImagePickerController is presented. Once it’s dismissed, a preview shows up with some options to add info.

Once the user submits the dog photo, everything is saved to a struct. The problem is that when the user taps the newDogButton a second time, the newDogView is never made visible again.

I think it might have something to do with the way I’m calling .removeFromSuperview().

Please let me know if anything stands out. I’m new to this!

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, CLLocationManagerDelegate, UITextFieldDelegate {

var image: UIImage?
var location: CLLocation?
var dogs: [Dog] = []
@IBOutlet var newDogButton: UIButton!
@IBOutlet var newDogScore: UILabel!
@IBOutlet var newDogName: UITextField!
@IBOutlet var newDogView: UIView!
@IBOutlet var preview: UIImageView!
@IBOutlet var map: MKMapView!
let locman = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    self.locman.delegate = self
    self.locman.requestWhenInUseAuthorization()
    self.locman.desiredAccuracy = kCLLocationAccuracyBest
    self.map.mapType = .standard
    self.map.showsUserLocation = true
    self.map.userTrackingMode = .follow
    self.newDogName.delegate = self

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    self.navigationController?.setNavigationBarHidden(true, animated: false)
}

@IBAction func newDogTapped(_ sender: Any) {
    presentCamera()
    self.locman.requestLocation()
}

func presentCamera() {
    let source = UIImagePickerControllerSourceType.camera
    guard UIImagePickerController.isSourceTypeAvailable(source)
        else {
            let alert = UIAlertController(title: "Camera Error", message: "Oops! Looks like Dog Spotter doesn't have access to your camera! Please open Settings to give Dog Spotter permission to use the camera.", preferredStyle: .alert)
            present(alert, animated: true)
            return
    }
    let camera = UIImagePickerController()
    camera.sourceType = source
    camera.delegate = self
    camera.allowsEditing = true

    self.present(camera, animated: true)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    image = info[UIImagePickerControllerOriginalImage] as? UIImage
    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
        image = editedImage
    }

    self.dismiss(animated: true, completion: {
        self.setupNewDogView()
    })
}

func setupNewDogView() {
    newDogView.isHidden = true
    map.isUserInteractionEnabled = false
    view.addSubview(newDogView)
    newDogView.translatesAutoresizingMaskIntoConstraints = false
    newDogView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: -50).isActive = true
    newDogView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 1, constant: -200).isActive = true
    newDogView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    newDogView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    preview.image = self.image
    newDogView.layer.cornerRadius = 25
    newDogButton.layer.cornerRadius = 25
    newDogView.isHidden = false
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let loc = locations.last!
    let coord = loc.coordinate
    location = loc
    print("You are at /(coord.latitude) /(coord.longitude)")
}

@IBAction func submitDog(_ sender: Any) {
    let newDog = Dog(name: newDogName.text!, score: Int(newDogScore.text!)!, picture: image!, location: location!)
    dogs.append(newDog)
    print(dogs.last!)
    UIView.animate(withDuration: 0.5, animations: {
        self.newDogView.alpha = 0
    }) { _ in
        self.newDogView.removeFromSuperview()
        self.map.isUserInteractionEnabled = true
    }
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    return
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

}

Here’s an image of my storyboard, if that’s helpful!

Here’s the console output when trying to display the view twice.

2017-08-10 16:27:59.263965-0700 DogSpotter[960:682273] – changing property contentsGravity in transform-only layer, will have no effect
2017-08-10 16:27:59.264702-0700 DogSpotter[960:682273] – changing property contentsGravity in transform-only layer, will have no effect
2017-08-10 16:27:59.374063-0700 DogSpotter[960:682273] libMobileGestalt MobileGestaltSupport.m:153: pid 960 (DogSpotter) does not have sandbox access for frZQaeyWLUvLjeuEK43hmg and IS NOT appropriately entitled
2017-08-10 16:27:59.374151-0700 DogSpotter[960:682273] libMobileGestalt MobileGestalt.c:550: no access to InverseDeviceID (see )
2017-08-10 16:28:03.321761-0700 DogSpotter[960:682273] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-08-10 16:28:03.322541-0700 DogSpotter[960:682273] [MC] Reading from public effective user settings.
>
You are at 45.4874888481593 -122.734139806728
Dog(name: “Rex”, score: 11, picture: size {750, 750} orientation 0 scale 1.000000, location: <+45.48748885,-122.73413981> +/- 65.00m (speed -1.00 mps / course -1.00) @ 8/10/17, 4:28:07 PM Pacific Daylight Time)
>
2017-08-10 16:28:44.979563-0700 DogSpotter[960:682273] [App] if we’re in the real pre-commit handler we can’t actually add any new fences due to CA restriction
2017-08-10 16:28:44.981668-0700 DogSpotter[960:682273] [App] if we’re in the real pre-commit handler we can’t actually add any new fences due to CA restriction
You are at 45.4874600607505 -122.734123729821

You have to set your self.newDogView.alpha back to 1 in setupNewDogView()
and dont use removeFromSuperView, you are lost the reference to the view.