iosobjective-cuipopover

io8 How to set UIPopoverPresentationController size


I was set the UIPopoverPresentationController to show the UIViewController and that like UIAlertView show.

But when I created the customized UIViewController and use the UIPopoverPresentationController. There was show full screen.

I want to build the effect like http://cocoa.tumblr.com/post/92070238973/how-can-i-use-both-uipopoverpresentationcontroller

I had try to set preferredContentSize, but it still show full screen.

my code below:

 - (void)viewDidLoad {
     [super viewDidLoad];
     contentVC = [UIViewController new];
     contentVC.view.backgroundColor = [UIColor darkGrayColor];
     contentVC.preferredContentSize = CGSizeMake(200, 200);


     UIPopoverPresentationController *popPC =      contentVC.popoverPresentationController;
     popPC.delegate = self;
     popPC.sourceView = self.view;
     popPC.sourceRect = CGRectMake(0,0,0,0);
     popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
 }

 - (IBAction)btnAction:(id)sender {

 [self presentViewController:contentVC animated:NO completion:ni

 }

 -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
 {
     return UIModalPresentationOverCurrentContext;
 }

Have anyone know how to set the size in the uiviewcontroller and the UIPopoverPresentationController like the website effect?

I will set other component in the UIViewController(now just set backgroundcolor).

Thank you very much.


Solution

  • Swift 3

    I made a numberpad that I wanted to use in this way, so on numberpad viewController, you need to set these values

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.preferredContentSize = CGSize(width:340, height:385) // IMPORTANT
    
        // REST ARE COSMETIC CHANGES
        self.view.backgroundColor = UIColor.clear
        self.view.isOpaque = true        
        self.view.layer.masksToBounds = false
        self.view.layer.shadowOffset = CGSize(width: 0, height: 5)
        self.view.layer.shadowColor = UIColor(red:0, green:0, blue:0, alpha:1).cgColor
        self.view.layer.shadowOpacity = 0.5
        self.view.layer.shadowRadius = 20
    }
    

    and on the viewController I am displaying it:

    func showNumpad(view: UIView)
        {
            let controller: UIViewController? = numberpadVC()
            // present the controller
            // on iPad, this will be a Popover
            // on iPhone, this will be an action sheet
            controller?.modalPresentationStyle = .popover
            self.present(controller!, animated: true, completion: { _ in })
            // configure the Popover presentation controller
            let popController: UIPopoverPresentationController? = controller?.popoverPresentationController
            popController?.permittedArrowDirections = .any
            //popController?.barButtonItem = textField.inputView
            popController?.sourceView = view
            popController?.delegate = self
            popController?.sourceRect = (controller?.view.frame)! //CGRect(x: 0, y:0, width:340, height:384)
        }
    

    displaying numberpad:

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool
    {
        //self.scrollview.addSubview(self.numpad)
        UIView.animate(withDuration: 0.25)
        {
            //self.numpad.frame.origin.x = self.viewNum1.frame.origin.x - 330
            if textField == self.txtNum1
            {
                self.showNumpad(view: textField)
            }
        }
    }
    

    and it is looking like this: enter image description here