Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in iOS8: UIPopoverController presentPopoverFromRect not work for keyWindow any more

As titled, in iOS8, [UIPopoverController presentPopoverFromRect] does not work for [UIApplication sharedApplication].keyWindow any more. (It does work in iOS7)

I verified following codes:

TestViewController *test = [[TestViewController alloc] initWithNibName:nil bundle:nil];

if (testPopoverController == nil) {
    testPopoverController = [[UIPopoverController alloc] initWithContentViewController:test];
    testPopoverController.popoverContentSize = CGSizeMake(250, 95*5);


}

CGPoint point = [sender convertPoint:CGPointMake(0, 0) toView:nil];
CGRect rect = CGRectMake(point.x, point.y, 24, 24);

[testPopoverController presentPopoverFromRect:rect inView:[UIApplication sharedApplication].keyWindow permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
like image 481
LiangWang Avatar asked Jul 27 '14 00:07

LiangWang


2 Answers

The best solution I've come up with is to handle it conditionally. If on iOS 7, use the working code we had for presenting a UIPopoverController on the key window. If on iOS 8, use the following:

     viewController.modalPresentationStyle = UIModalPresentationPopover;
     UIPopoverPresentationController *presentationController = viewController.popoverPresentationController;
     [presentationController setDelegate:self];
     presentationController.permittedArrowDirections = 0;
     presentationController.sourceView = [[UIApplication sharedApplication] keyWindow];
     presentationController.sourceRect = [[UIApplication sharedApplication] keyWindow].bounds;

     [viewController setPreferredContentSize:CGSizeMake(320, 480)];
     [parentController presentViewController:viewController animated:YES completion:nil];

This ends up functioning identically to:

     self.popoverController = [[UIPopoverController alloc] initWithContentViewController:viewController];
     [self.popoverController setDelegate:self];
     [self.popoverController setPopoverContentSize:isLandscape() ? CGSizeMake(480*2, 320*2) : CGSizeMake(320*2, 480*2)];
     [self.padPopover presentPopoverFromRect:CGSizeMake(320, 480)
                                      inView:[[UIApplication sharedApplication] keyWindow]
                    permittedArrowDirections:0
                                    animated:YES];
like image 183
Matt Foley Avatar answered Nov 11 '22 10:11

Matt Foley


Change the inView parameter in

[testPopoverController presentPopoverFromRect:rect inView:[UIApplication sharedApplication].keyWindow permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

to

[testPopoverController presentPopoverFromRect:rect inView:"your current View Controller's view" permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

And make sure it is in dispatch block

  dispatch_async(dispatch_get_main_queue(), ^{

});

Also kip changing your rect origin n size values if it is within the bounds

like image 31
sheetal Avatar answered Nov 11 '22 11:11

sheetal