本文介绍了在iOS8中:UIPopoverController presentPopoverFromRect不再适用于keyWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题为,在iOS8中,[UIPopoverController presentPopoverFromRect]不再适用于[UIApplication sharedApplication] .keyWindow。 (它在iOS7中有效)

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

我验证了以下代码:

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


推荐答案

我提出的最佳解决方案是有条件地处理它。如果在iOS 7上,使用我们在关键窗口上呈现UIPopoverController的工作代码。如果在iOS 8上,请使用以下内容:

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

这篇关于在iOS8中:UIPopoverController presentPopoverFromRect不再适用于keyWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 01:03