iOS8:不显示UIPopoverController,但iOS7没问题

时间:2015-01-21 07:41:00

标签: ios ios7 ios8

自iOS8以来,Apple似乎改变了UIPopoverController的态度。

请查看下面的代码。我想在addSubview之后显示UIPopoverController。 iOS6和iOS7没有问题,但它不会在iOS8上显示。

你能否提出任何可能的原因?任何帮助将不胜感激。

- (IBAction)tapButton:(id)sender {

    SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
                                                                          [UIScreen mainScreen].bounds.size.width,
                                                                          [UIScreen mainScreen].bounds.size.height)];
    sampleView.backgroundColor = [UIColor blueColor];
    sampleView.alpha = 0.5;

    sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2,
                                                                 [UIScreen mainScreen].bounds.size.height / 2,
                                                                 200.0f, 20.0f)];
    sampleView.label.text = @"SAMPLE TEXT";
    sampleView.label.textColor = [UIColor redColor];
    [sampleView addSubview:sampleView.label];

    [self.view.window addSubview:sampleView];

    if (!self.popover) {
        UIViewController *vc = [[UIViewController alloc] init];
        vc.view.backgroundColor = [UIColor redColor];
        vc.preferredContentSize = CGSizeMake(200, 300);
        self.popover = [[UIPopoverController alloc] initWithContentViewController:vc];
    }

    // On iOS7 is OK but not show on iOS8
    [self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0)
                                  inView:sampleView.label
                permittedArrowDirections:UIPopoverArrowDirectionUp
                                animated:YES];
}

enter image description here

enter image description here

UPDATE1:

我接着回答了这个问题的答案。

in iOS8: UIPopoverController presentPopoverFromRect not work for keyWindow any more

这是更新的代码。

[self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0)
                                  inView:self.view
                permittedArrowDirections:UIPopoverArrowDirectionUp
                                animated:YES];

enter image description here

据我检查,keyWindos和self.view.window似乎是一样的。

NSLog(@"%@", [UIApplication sharedApplication].keyWindow);
NSLog(@"%@", self.view.window);

但是,这不是我案例的完整解决方案。我想控制添加的子视图的视图。

UPDATE2:

我想我需要在iOS8上使用UIPopoverPresentationController。

- (IBAction)tapButton:(id)sender {

    // View
    SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
                                                                          [UIScreen mainScreen].bounds.size.width,
                                                                          [UIScreen mainScreen].bounds.size.height)];
    sampleView.backgroundColor = [UIColor blueColor];
    sampleView.alpha = 0.5;

    // Label
    sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 20.0f)];
    sampleView.label.text = @"SAMPLE TEXT";
    sampleView.label.textColor = [UIColor redColor];
    [sampleView addSubview:sampleView.label];

    // View Controller
    UIViewController *viewController = [[UIViewController alloc] init];
    viewController.modalPresentationStyle = UIModalPresentationPopover;
    viewController.view = sampleView;

    // UIPopoverPresentationController
    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)];
    [self presentViewController:viewController animated:YES completion:nil];

    if (!self.popover) {
        UIViewController *vc = [[UIViewController alloc] init];
        vc.view.backgroundColor = [UIColor redColor];
        vc.preferredContentSize = CGSizeMake(200, 300);
        vc.modalPresentationStyle = UIModalPresentationPopover;
        self.popover = [[UIPopoverController alloc] initWithContentViewController:vc];
        self.popover.delegate = self;
    }

    [self.popover presentPopoverFromRect:CGRectMake(300, 300, 0, 0)
                                  inView:viewController.view
                permittedArrowDirections:UIPopoverArrowDirectionUp
                                animated:YES];
}

enter image description here

Update3我使用了dispatch_async但没有工作。有人说dispatch_async在这种情况下是有效的。 dispatch_async是一个非常简单的解决方案。我更喜欢用它。如果你可以通过使用dispatch_async解决我的问题,我将不胜感激你可以附上代码。

- (IBAction)tapButton:(id)sender {

    SampleView *sampleView = [[SampleView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
                                                                          [UIScreen mainScreen].bounds.size.width,
                                                                          [UIScreen mainScreen].bounds.size.height)];
    sampleView.backgroundColor = [UIColor blueColor];
    sampleView.alpha = 0.5;

    sampleView.label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2,
                                                                 [UIScreen mainScreen].bounds.size.height / 2,
                                                                 200.0f, 20.0f)];
    sampleView.label.text = @"SAMPLE TEXT";
    sampleView.label.textColor = [UIColor redColor];
    [sampleView addSubview:sampleView.label];

    [self.view.window addSubview:sampleView];

    if (!self.popover) {
        UIViewController *vc = [[UIViewController alloc] init];
        vc.view.backgroundColor = [UIColor redColor];
        vc.preferredContentSize = CGSizeMake(200, 300);
        self.popover = [[UIPopoverController alloc] initWithContentViewController:vc];
    }

    // I tried this but not worked.
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0)
                                      inView:sampleView.label
                    permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    });
}

1 个答案:

答案 0 :(得分:3)

尝试为iOS 8执行以下操作

   dispatch_async( dispatch_get_main_queue(), ^{
    [self.popover presentPopoverFromRect:CGRectMake(200, 100, 0, 0) inView:sampleView.label permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
});

修改

这是我的代码,它可以使用或不使用dispatch_async。

 self.popover = [[UIPopoverController alloc] initWithContentViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"second"]];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.popover presentPopoverFromRect:self.button.frame inView:self.button permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    });