Search code examples
iosiphoneswiftipadpopover

Popover image gallery not working in iPad iOS Swift


Recently my app has rejected from App Review because I wasn't using Popover. then I changed my coding into following. but still I'm not getting popup window in simulator.

Always getting normal iPhone photo choosing method and it makes app crash.

Also its not even printing "working".

 @IBAction func chooseGallery(sender: UIBarButtonItem) {

    imagePicker.sourceType = .PhotoLibrary

  //imagePicker.modalPresentationStyle = .Popover
  //presentViewController(imagePicker, animated: true, completion: nil)//4
  //imagePicker.popoverPresentationController?.barButtonItem = sender

    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }
    else {
        println("Working")// to test this part

        imagePicker.modalPresentationStyle = .Popover
        presentViewController(imagePicker, animated: true, completion: nil)//4
        imagePicker.popoverPresentationController?.barButtonItem = (sender)

        imagePicker.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up
        imagePicker.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)
     }     
}

Solution

  • Your code works fine for me with some modifications, In the following example I going to present a Popover only for iPad just like you.

    @IBAction func showNextViewController(sender: UIBarButtonItem) {
    
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("NextViewController") as! NexViewController
    
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            self.presentViewController(nextViewController, animated: true, completion: nil)
        }
        else {            
            nextViewController.modalPresentationStyle = .Popover
            presentViewController(nextViewController, animated: true, completion: nil)
    
            if let popover = nextViewController.popoverPresentationController {             
                popover.barButtonItem = sender
                popover.permittedArrowDirections = UIPopoverArrowDirection.Up
    
                // to set it size
                nextViewController.preferredContentSize = CGSizeMake(200,500)
            }
        }    
    }
    

    Because I don't know the class of UIViewController you're trying to present I just made one myself without nothing inside, and I instantiate it in the @IBAction to avoid keep references to it (is just for test purposes).

    Just some observations:

    • When you're going to present popovers you need to set only one of the two follwoing:

      • barButtonItem
      • sourceView, sourceRect

    In the above example just like you set the barButtonItem you don't need anything more.

    I hope this help you.