io8 How to set UIPopoverPresentationController size
Asked Answered
I

3

13

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.

Infusorian answered 25/2, 2015 at 0:40 Comment(4)
Perhaps this will help: https://mcmap.net/q/101183/-uipopoverpresentationcontroller-on-ios-8-iphoneCrenelation
I had saw the post, but it's not help me. I had try to convert the objective-c like above code, but the popover size not changed.Infusorian
I wasn't referring to the code portion of the answer but the use of adaptivePresentationStyleForPresentationController: -- developer.apple.com/library/prerelease/ios/documentation/UIKit/…:Crenelation
I had try to changed adaptivePresentationStyleForPresentationController return value, I had try all the return constant , but it's still no changed:(Infusorian
H
5

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

Hamitosemitic answered 22/9, 2017 at 5:30 Comment(0)
M
12

In storyboard select the Navigation Controller and set Content Size values.

enter image description here

This can also be done in code by subclassing UINavigationController and setting preferredContentSize.

- (void)viewDidLoad {
   [super viewDidLoad];
   self.preferredContentSize = CGSizeMake(100, 100);
}
Millardmillboard answered 18/7, 2016 at 21:45 Comment(0)
H
5

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

Hamitosemitic answered 22/9, 2017 at 5:30 Comment(0)
D
3
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
    return UIModalPresentationNone;
}

Set this under your code.

Dipody answered 5/1, 2016 at 2:52 Comment(2)
it's necessary in iphone! but no need in pad.Give
Can you please explain how the size of the UIPopoverPresentationController is set by doing this.Operatic

© 2022 - 2024 — McMap. All rights reserved.