Search code examples
iosipaduipopovercontroller

Unable to center UIPopoverController


In my app I'm trying to center a UIPopoverController (I've to use it because my app it's compatible with iOS 7). I'm trying to center it without success. My code to center it's the follow:

        SummaryViewController *summaryViewController = [[SummaryViewController alloc] init];
        summaryViewController.delegate = self;
        aPopover = [[UIPopoverController alloc] initWithContentViewController:summaryViewController];
        aPopover.popoverContentSize = CGSizeMake(summaryViewController.view.frame.size.width, summaryViewController.view.frame.size.height);

        CGFloat width = [[UIScreen mainScreen] bounds].size.width;
        CGFloat height = [[UIScreen mainScreen] bounds].size.height;
        CGFloat popoverWidth = summaryViewController.view.frame.size.width;
        CGFloat popoverHeight = summaryViewController.view.frame.size.height;

        CGRect rect = CGRectMake((width - popoverWidth) / 2.0, (height -popoverHeight) / 2.0, 1, 1);
        [aPopover presentPopoverFromRect:rect inView:self.view permittedArrowDirections:0 animated:YES];

It always shows me the popover in the left corner... What's wrong with this code?


Solution

  •   SummaryViewController *summaryViewController = [[SummaryViewController alloc] init];
      summaryViewController.delegate = self;
      aPopover = [[UIPopoverController alloc] initWithContentViewController:summaryViewController];
      aPopover.popoverContentSize = CGSizeMake(summaryViewController.view.frame.size.width, summaryViewController.view.frame.size.height);
    
      CGFloat width = [[UIScreen mainScreen] bounds].size.width;
      CGFloat height = [[UIScreen mainScreen] bounds].size.height;
      CGFloat popoverWidth = summaryViewController.view.frame.size.width;
      CGFloat popoverHeight = summaryViewController.view.frame.size.height;
    
      CGRect rect = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 1, 1);
      [aPopover presentPopoverFromRect:rect inView:self.view permittedArrowDirections:0 animated:YES];