in iOS8: UIPopoverController presentPopoverFromRect not work for keyWindow any more

后端 未结 4 1529
臣服心动
臣服心动 2021-02-05 12:03

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

I verifi

4条回答
  •  野性不改
    2021-02-05 12:49

    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];
    

提交回复
热议问题