ImagePicker Confirmation View Alignment

Asked by At

I'm using UIImagePickerController to take a picture. After taking a the picture I'm presented with a view to confirm the shot or retake. This view is not align the same way, it seems like it is aligned to the bottom of the screen. Is there a way to align both views to the top of the screen or can I skip this step?

Screenshot of both views

imagePickerController = UIImagePickerController()
imagePickerController.delegate = self

//check if camera is available
if UIImagePickerController.isSourceTypeAvailable(.camera) {
    imagePickerController.sourceType = .camera
} else {
    imagePickerController.sourceType = .photoLibrary
}

self.present(imagePickerController, animated: true, completion: nil)

1 Answers

0
ajeferson On

Well, you can not display that confirmation screen by using custom controls. This is a pretty simple example:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

  @IBOutlet weak var textField: UITextField!
  var imagePickerController: UIImagePickerController!

  override func viewDidLoad() {
    super.viewDidLoad()

    guard UIImagePickerController.isSourceTypeAvailable(.camera) else { return }
    imagePickerController = UIImagePickerController()
    imagePickerController.sourceType = .camera
    imagePickerController.showsCameraControls = false
    imagePickerController.delegate = self

    // Button to take picture
    let takePictureButton = UIButton()
    takePictureButton.setTitle("Take Picture", for: .normal)
    imagePickerController.view.addSubview(takePictureButton)

    // Positioning button
    takePictureButton.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
      takePictureButton.centerXAnchor.constraint(equalTo: imagePickerController.view.centerXAnchor),
      takePictureButton.bottomAnchor.constraint(equalTo: imagePickerController.view.bottomAnchor, constant: -50)
      ])

    // Set button target
    takePictureButton.addTarget(self, action: #selector(didTouchTakePictureButton(_:)), for: .touchUpInside)

    self.present(imagePickerController, animated: true, completion: nil)
  }

  @objc func didTouchTakePictureButton(_ sender: UIButton) {
    imagePickerController.takePicture()
  }

  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    guard let image = info[.originalImage] as? UIImage else { return }
  }
}

Beware of the following, though: 1. You will be only allowed to use .camera for the source type; 2. You have no controls whatsoever, so you have to add all the customization yourself;

Hope this helps!