skip to Main Content

I have created a Image Picker UI where I want to do selection when user clicked on the button,
I have done it using UIAlertController and when I test it in iPhone then it works perfect but,
when I test it in iPad then after button Clicked, the app crashed.

How do I make the UIAlertController compatible for iPad also?

3

Answers


  1. Chosen as BEST ANSWER

    We can use

    let ac = UIAlertController(title: nil, message: nil, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? .alert : .actionSheet)
    

    Full Code in swift

       @IBAction func pickBTN(_ sender: UIButton) {
    
    
        let ac = UIAlertController(title: nil, message: nil, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? .alert : .actionSheet)
    
     
        ac.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { action in
            self.gallery()
        }))
        ac.addAction(UIAlertAction(title: "Camera", style: .default, handler: { action in
            self.camera()
        }))
        ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
    
        }))
        
        present(ac, animated: true)
    
    
    }
    

    Functions that are used in above code (for camera & Gallery)

    func gallery (){
        imagePicker.allowsEditing = false
        imagePicker.sourceType = .savedPhotosAlbum
        present(imagePicker, animated: true, completion: nil)
    }
    
    func camera (){
        imagePicker.allowsEditing = false
        imagePicker.sourceType = .camera
        present(imagePicker, animated: true, completion: nil)
    }
    

  2. Extension for UIViewController

      extension UIViewController{
                public func addAlertForiPad(alert: UIAlertController) {
                    if let alertController = alert.popoverPresentationController {
                        alertController.sourceView = view
                        alertController.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
                        alertController.permittedArrowDirections = []
                    }
                }
            }
    

    Usages

        let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
        
          if UIDevice.current.userInterfaceIdiom == .pad {
               addAlertForiPad(alert: alertController)
           }
    
          alertController.popoverPresentationController?.sourceView = view
    
          alertController.addAction(UIAlertAction(title: "Approve", style: .default , handler:{ (UIAlertAction)in
               print("User click Approve button")
           }))
                        
          alertController.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in
                            print("User click Dismiss button")
           }))
                    
          present(alertController, animated: true, completion: nil)
    
    Login or Signup to reply.
  3. On an iPad a uialertcontroller must be shown relative to some other controller because you can’t just show it “anywhere on the screen”.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search