Search code examples
iosobjective-cuipopovercontrollerpopover

Popover presentation on an iPhone using UIPopoverPresentationController


Hello everyone I want to make a popover for iPhone so here is my code where I am geting wrong please suggest I want to create somthing like below pic but mine is covering the whole screen and I dont want to use segue .So basicaly I want if I click on button a resized View controller should popup how can I achieve this please help.I had followed this tutorial. https://www.youtube.com/watch?v=UQBbJQNEDA4 enter image description here

#import "ViewController.h"
#import "Popup.h"

@interface ViewController ()<UIPopoverPresentationControllerDelegate>
{
    Popup * popupController;
    UIPopoverPresentationController *popupPresentationController;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)popupOnClick:(id)sender {

    popupController=[self.storyboard instantiateViewControllerWithIdentifier:@"Popup"];
    popupController.modalPresentationStyle=UIModalPresentationPopover;
    popupPresentationController= [popupController popoverPresentationController];
    popupPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    popupController.preferredContentSize=CGSizeMake(150, 300);
    popupPresentationController.delegate = self;

    [self presentViewController:popupController animated:YES completion:NULL];


    // in case we don't have a bar button as reference
    popupPresentationController.sourceView = _popupButton;
    popupPresentationController.sourceRect = _popupButton.frame;
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
    return UIModalPresentationNone;
}

@end

Solution

  • Here's your problem:

    [self presentViewController:popupController animated:YES completion:nil];
    popupPresentationController= [popupController popoverPresentationController];
    popupPresentationController.delegate = self;
    

    That code is in the wrong order. You must set the delegate before calling presentViewController.