Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues in PopoverView Controller preferredContentSize

I want to display PopoverViewController in centre with preferredContentSize(W: 250, H: 50), But it displays full size of that ViewController, Please find me where I done mistake and correct it out.

class ViewController: UIViewController, 
    UIPopoverControllerDelegate, UIPopoverPresentationControllerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func PopBtn_Action(_ sender: Any) {

    let popController = self.storyboard!.instantiateViewController(withIdentifier: "PincodeViewController") as! PincodeViewController

    // set the presentation style
    popController.modalPresentationStyle = UIModalPresentationStyle.popover

    // set up the popover presentation controller
    popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
    popController.popoverPresentationController?.delegate = self
    popController.popoverPresentationController?.sourceView = sender as? UIView // button
    popController.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.width/2, y: self.view.bounds.height/2, width: 1, height: 1)

    popController.preferredContentSize = CGSize(width: 250, height: 250)

    // present the popover
    self.present(popController, animated: true, completion: nil)

}


func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}

func popoverPresentationControllerShouldDismissPopover(popoverPresentationController: UIPopoverPresentationController) -> Bool {
    return true
}

func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {

}
like image 775
Harikrishnan Avatar asked Oct 24 '25 04:10

Harikrishnan


2 Answers

Try the following code in swift 3.

func toShowPopOver()
{
   let popoverContent = self.storyboard!.instantiateViewController(withIdentifier: "PincodeViewController") as! PincodeViewController
   popoverContent.modalPresentationStyle = .popover
   if let popover = popoverContent.popoverPresentationController {
      popover.sourceView = self.view
      popover.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0)
      popover.sourceRect = CGRect(x: self.view.bounds.width/2, y: self.view.bounds.height/2, width: 1, height: 1)
      popoverContent.preferredContentSize = CGSize(width: 50, height: 100)
      popover.delegate = self
   }

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


func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {

   return UIModalPresentationStyle.none
}
like image 86
McDonal_11 Avatar answered Oct 26 '25 18:10

McDonal_11


change your delegate method with this

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle
{
    return UIModalPresentationStyle.none
}

It worked for me.. Hope this will help to you..

like image 36
Rohan Dave Avatar answered Oct 26 '25 17:10

Rohan Dave