How to show Actionsheet in iPad

remy boys picture remy boys · Aug 9, 2016 · Viewed 14.4k times · Source

How can I show a UIActionsheet in iPad when I'm using my current code its giving me this error:

Your application has presented a UIAlertController (<UIAlertController: 0x7f9ec624af70>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.

which is working totally fine in an iPhone :

let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
let reminderAction = UIAlertAction(title: "Reminder", style: .Default, handler: {
                (alert: UIAlertAction!) -> Void in }
optionMenu.addAction(reminderAction)
self.presentViewController(optionMenu, animated: true, completion: nil)

I came across some similar problems, the solution was this:

let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
optionMenu.popoverPresentationController?.sourceView = self.view
optionMenu.popoverPresentationController?.sourceRect = self.view.bounds

but it didnt worked for me maybe because my ActionSheet's Sender is on a UItableviewCell.

I tired to set AlertController's Sourceview to tableView's Cell but its not correctly placed and sometime its partially visible this is what I tried:

optionMenu.popoverPresentationController?.sourceView = currentCell.contentView
optionMenu.popoverPresentationController?.sourceRect = currentCell.contentView.bounds

Any clue how can I fix this problem?

Answer

Prashant Ghimire picture Prashant Ghimire · Aug 10, 2016

The sample code given below works both on iPhone and iPad.

 guard let viewRect = sender as? UIView else {
            return
        }

    let cameraSettingsAlert = UIAlertController(title: NSLocalizedString("Please choose a course", comment: ""), message: NSLocalizedString("", comment: ""), preferredStyle: .ActionSheet)
    cameraSettingsAlert.modalPresentationStyle = .Popover

    let photoResolutionAction = UIAlertAction(title: NSLocalizedString("Photo Resolution", comment: ""), style: .Default) { action in

    }
    let cameraOrientationAction = UIAlertAction(title: NSLocalizedString("Camera Orientation", comment: ""), style: .Default) { action in

    }
    let flashModeAction = UIAlertAction(title: NSLocalizedString("Flash Mode", comment: ""), style: .Default) { action in

    }
    let timeStampOnPhotoAction = UIAlertAction(title: NSLocalizedString("Time Stamp on Photo", comment: ""), style: .Default) { action in

    }
    let cancel = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .Cancel) { action in

    }
    cameraSettingsAlert.addAction(cancel)
    cameraSettingsAlert.addAction(cameraOrientationAction)
    cameraSettingsAlert.addAction(flashModeAction)
    cameraSettingsAlert.addAction(timeStampOnPhotoAction)
    cameraSettingsAlert.addAction(photoResolutionAction)

    if let presenter = cameraSettingsAlert.popoverPresentationController {
        presenter.sourceView = viewRect;
        presenter.sourceRect = viewRect.bounds;
    }
    presentViewController(cameraSettingsAlert, animated: true, completion: nil)