在iOS8中:UIPopoverController presentPopoverFromRect不再适用于keyWindow

时间:2014-07-27 00:20:05

标签: uipopovercontroller ios8

标题为,在iOS8中,[UIPopoverController presentPopoverFromRect]不再适用于[UIApplication sharedApplication] .keyWindow。 (它在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];

4 个答案:

答案 0 :(得分:3)

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

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

这最终与以下功能完全相同:

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

答案 1 :(得分:1)

更改

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

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

确保它在调度块中

  dispatch_async(dispatch_get_main_queue(), ^{

});

如果它在范围内

,也可以改变你的矩形原点n大小值

答案 2 :(得分:0)

尝试此链接中的解决方案:http://github.com/werner77/WEPopover

1,让我们来做这段代码:

if ([viewController respondsToSelector:@selector(setPreferredContentSize:)]) {
    viewController.preferredContentSize = CGSizeMake(200, 300.0f);
} else {
    viewController.contentSizeForViewInPopover = CGSizeMake(200.0f, 300.0f);
}

self.popoverController = [[popoverClass alloc] initWithContentViewController:viewController];

if ([self.popoverController respondsToSelector:@selector(setContainerViewProperties:)]) {
    [self.popoverController setContainerViewProperties:[self improvedContainerViewProperties]];
}

self.popoverController.delegate = self;

[self.popoverController presentPopoverFromRect:[sender bounds]
      inView:sender
      permittedArrowDirections:UIPopoverArrowDirectionUp
      animated:YES];

我希望这会有所帮助。它已经过测试,适用于iOS6-iOS8。

PS。不要忘记检查viewDidLoad,并感谢WEPopover库。

答案 3 :(得分:-1)

我认为你必须在全视图/ viewcontroller中显示它,而不是在[UIApplication sharedApplication] .keyWindow

中显示它

所以改变这段代码:

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

为:

[testPopoverController presentPopoverFromRect:rect inView:aViewControllerInYourApp permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];