Solution for Ch 15 Gold Challenge in Xcode 9.3


#1
//  DetailViewController.swift

class DetailViewController: UIViewController {
...
  @IBAction func takePicture(_ sender: UIBarButtonItem) {
    let imagePicker = UIImagePickerController()
    if UIImagePickerController.isSourceTypeAvailable(.camera) {
      imagePicker.sourceType = .camera

      imagePicker.showsCameraControls = false

      let overlayView = UIView(frame: CGRect(origin: .zero, size: UIScreen.main.bounds.size))
      let viewFinderView = UIView(frame: overlayView.frame)
      viewFinderView.frame.size.height = viewFinderView.frame.size.width * 4/3
      overlayView.addSubview(viewFinderView)

      let crosshairLabel = UILabel()
      crosshairLabel.numberOfLines = 0
      crosshairLabel.text = "\u{231C}    \u{231D}\r\u{231E}    \u{231F}"
      crosshairLabel.font = UIFont.systemFont(ofSize: 30)
      crosshairLabel.textColor = UIColor.green
      crosshairLabel.translatesAutoresizingMaskIntoConstraints = false
      viewFinderView.addSubview(crosshairLabel)
      crosshairLabel.centerXAnchor.constraint(equalTo: viewFinderView.centerXAnchor).isActive = true
      crosshairLabel.centerYAnchor.constraint(equalTo: viewFinderView.centerYAnchor).isActive = true

      let snapButton = UIButton(type: .system)
      snapButton.setTitle("Snap", for: .normal)
      snapButton.isSelected = true
      snapButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 30)
      snapButton.translatesAutoresizingMaskIntoConstraints = false
      overlayView.addSubview(snapButton)
      snapButton.centerXAnchor.constraint(equalTo: overlayView.centerXAnchor).isActive = true
      snapButton.bottomAnchor.constraint(equalTo: overlayView.bottomAnchor, constant: -40).isActive = true
      snapButton.addTarget(imagePicker, action: #selector(UIImagePickerController.takePicture), for: .touchUpInside)

      imagePicker.cameraOverlayView = overlayView
    } else {
      imagePicker.sourceType = .photoLibrary
    }

    imagePicker.delegate = self

    present(imagePicker, animated: true, completion: nil)
  }
}