Pawan Dhakar
Pawan Dhakar

Reputation: 13

Action Sheet is not working in ipad ios 13.6

My app uses UIAlertController for both ActionSheet and Alert. It works fine on iOS 13.4 in iPad, but if I run the code from my iOS 13.6 device, this is not working properly in iPad 12.9 ios 13.6.

    let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle: 
    UIAlertController.Style.actionSheet)
       alert.addAction(UIAlertAction(title: "Camera", style: UIAlertAction.Style.default, 
    handler: { (res) in
        self.btnClickedCamera(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Gallery", style: UIAlertAction.Style.default, 
    handler: { (res) in
        self.btnClickedGallery(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (res) in
        
    }))
    if let popoverPresentationController = alert.popoverPresentationController {
        popoverPresentationController.sourceView = sender
        popoverPresentationController.sourceRect = sender.bounds
    }
    self.present(alert, animated: true, completion: nil)

Upvotes: 1

Views: 1670

Answers (3)

Ahmed Ghzawi
Ahmed Ghzawi

Reputation: 21

let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle: 
    UIAlertController.Style.actionSheet)
       alert.addAction(UIAlertAction(title: "Camera", style: UIAlertAction.Style.default, 
    handler: { (res) in
        self.btnClickedCamera(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Gallery", style: UIAlertAction.Style.default, 
    handler: { (res) in
        self.btnClickedGallery(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (res) in
        
    }))
  if UIDevice.current.userInterfaceIdiom == .pad {
               if let popup = alert.popoverPresentationController {
                   popup.sourceView = self.view
                   popup.sourceRect = CGRect(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 4, width: 0, height: 0)
               }
           }
    }
    self.present(alert, animated: true, completion: nil)

Upvotes: 2

Ashutosh Mishra
Ashutosh Mishra

Reputation: 1939

I have tried your code on iPhone(Device:- iPhone 11 Pro 13.6) and iPad(iPad pro (12.9- 4th generation) both and it works. But If you said, I have changed some popover frame, Use the following code:-

   let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle:
    UIAlertController.Style.actionSheet)
       alert.addAction(UIAlertAction(title: "Camera", style: UIAlertAction.Style.default,
    handler: { (res) in
        self.btnClickedCamera(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Gallery", style: UIAlertAction.Style.default,
    handler: { (res) in
       self.btnClickedGallery(tag:2)
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (res) in

    }))
    if let popoverPresentationController = alert.popoverPresentationController {

        popoverPresentationController.sourceRect = sender.frame
        popoverPresentationController.sourceView = self.view

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

}

Upvotes: 1

Bunthoeun
Bunthoeun

Reputation: 162

Try below:

 func showAlert(vc: UIViewController) {
      let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle: .actionSheet)
      alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (res) in
         //TODO: your action
      }))
      alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (res) in
         //TODO: your action
      }))
      if let popoverController = alert.popoverPresentationController {
          popoverController.sourceView = vc.view
          popoverController.sourceRect = CGRect(x: vc.view.bounds.midX, y: vc.view.bounds.midY, width: 0, height: 0)
          popoverController.permittedArrowDirections = []
      }
      let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
          alert.dismiss(animated: true, completion: nil)
      }
      vc.present(alert, animated: true, completion: nil)
  }
 

Upvotes: 2

Related Questions