如何在iPad中显示Actionsheet

rem*_*oys 8 ipad ios swift uialertcontroller

UIActionsheet当我使用当前代码时,如何在iPad中显示它给我这个错误:

您的应用程序已呈现样式UIAlertController(<UIAlertController: 0x7f9ec624af70>)UIAlertControllerStyleActionSheet.在modalPresentationStyle一个UIAlertController与这种风格UIModalPresentationPopover.您必须通过警报控制器提供此弹出窗口的位置信息popoverPresentationController.你必须提供一个sourceViewsourceRectbarButtonItem.如果在显示警报控制器时不知道此信息,您可以在UIPopoverPresentationControllerDelegate方法中提供该信息-prepareForPopoverPresentation.

这在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)
Run Code Online (Sandbox Code Playgroud)

我遇到了一些类似的问题,解决方法是:

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

但它并没有对我有用,可能是因为我的ActionSheet的发件人在UItableviewCell上.

我厌倦了将AlertController的Sourceview设置为tableView的Cell,但它没有正确放置,有时它部分可见这是我试过的:

optionMenu.popoverPresentationController?.sourceView = currentCell.contentView
optionMenu.popoverPresentationController?.sourceRect = currentCell.contentView.bounds
Run Code Online (Sandbox Code Playgroud)

任何线索如何解决这个问题?

Pra*_*ire 10

下面给出的示例代码适用于iPhone和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)
Run Code Online (Sandbox Code Playgroud)


小智 7

如果你想在 iPad 上显示任何 ActionSheet,那么他们使用 popoverPresentationController 来显示并且在 iPad 上不显示操作表的取消样式按钮。在 Swift 3 中使用此代码:

  @IBAction func ActionSheetShow(_ sender: UIButton) {

  let actionSheet = UIAlertController(title: "Choose any option", message: "choose as you like here!", preferredStyle: .actionSheet)

    actionSheet.addAction(UIAlertAction(title: "Click1", style: .cancel, handler: {
    action in
        print("first button")
    }))
    actionSheet.addAction(UIAlertAction(title: "Click2", style: .default, handler: {
        action in
        print("second button")
    }))
    actionSheet.addAction(UIAlertAction(title: "Click3", style: .destructive, handler: {
        action in
        print("third button")
    }))
    actionSheet.popoverPresentationController?.sourceView = self.view
    actionSheet.popoverPresentationController?.sourceRect = sender.frame
    present(actionSheet, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

祝你好运!