ActionSheet not working iPad

后端 未结 8 2075
粉色の甜心
粉色の甜心 2020-12-02 16:25

I\'m using ActionSheet in my application. On my iPhone it works, but it doesn\'t on the iPad simulator.

this is my code:

@IBAction func dialog(send         


        
相关标签:
8条回答
  • 2020-12-02 16:45

    add statements in the following terms before presented.

    optionMenu.popoverPresentationController.sourceView = self.view;
    optionMenu.popoverPresentationController.sourceRect = 
    
    CGRectMake(0,0,1.0,1.0);
    
    
    @IBAction func dialog(sender: AnyObject) {
        ...
    
        optionMenu.popoverPresentationController.sourceView = self.view;
        optionMenu.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0);
    
        self.presentViewController(optionMenu, animated: true, completion: nil)
    }
    

    it will work well.

    0 讨论(0)
  • 2020-12-02 16:46

    Swift 3

    As said before, you should configure UIAlertController to be presented on a specific point on iPAD.

    Example for navigation bar:

        // 1
        let optionMenu = UIAlertController(title: nil, message: "Choose an option", preferredStyle: .actionSheet)
    
        // 2
        let deleteAction = UIAlertAction(title: "Option 1", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            print("option 1 pressed")
        })
        let saveAction = UIAlertAction(title: "Option 2", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            print("option 2 pressed")
        })
    
        //
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
            (alert: UIAlertAction!) -> Void in
            print("Cancelled")
        })
    
    
        // 4
    
        optionMenu.addAction(deleteAction)
        optionMenu.addAction(saveAction)
        optionMenu.addAction(cancelAction)
    
        // 5
    
        optionMenu.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
    
        self.present(optionMenu, animated: true) { 
            print("option menu presented")
        }
    
    0 讨论(0)
  • 2020-12-02 16:49

    you need to add this for Ipad

    alertControler.popoverPresentationController?.sourceView = self.view

    0 讨论(0)
  • 2020-12-02 16:59

    If you wish to present it in the centre with no arrows [Swift 3+]:

    if let popoverController = optionMenu.popoverPresentationController {
            popoverController.sourceView = self.view
            popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
            popoverController.permittedArrowDirections = []
        }
    self.present(optionMenu, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-02 17:05

    Same problem for me. I had a UIAlertController that worked fine on phone, but crashed on iPad. The sheet pops up when a cell is tapped from a table view.

    For Swift 3, I added 3 lines of code right before presenting it:

            ...
    
            sheet.popoverPresentationController?.sourceView = self.view
            sheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection()
            sheet.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
    
    
            self.present(sheet, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-02 17:07

    I've done this package to manage ActionSheet and Popover in iPhone, Ipad and Mac. https://github.com/AndreaMiotto/ActionOver

    0 讨论(0)
提交回复
热议问题