如何在iOS7中禁用UIPopoverController中的较暗透明效果?

7

我使用UIPopoverController在iPad iOS7中弹出视图,如下所示:

    if (!self.popover) {
        UIViewController *popupVC = [[UIViewController alloc] init];
        [popupVC.view addSubview:thePopupView];
        popupVC.preferredContentSize = CGSizeMake(240, 140);
        self.popover = [[UIPopoverController alloc] initWithContentViewController:popupVC];
        self.popover.delegate = self;
    }


    [self.popover presentPopoverFromBarButtonItem:barButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

但是当弹出窗口激活时,在iOS6中会使屏幕变暗,而这个效果不会影响其他视图。

如何解决这个问题呢?谢谢!

2个回答

4

如果你指的是插入在弹出窗口下方的模糊视图,那么只有一个变通方法 - 使用自定义popoverBackgroundViewClass

虽然有些复杂,但并不像你想象的那么难。


我的弹出内容只是一个小矩形(240,140)。但是暗效果影响整个屏幕。它不是阴影视图。 - LE SANG
@UFO 能否给我们展示一下屏幕截图?没有看到实际效果很难理解。我实际上并不是指“阴影”,而是要说“调暗视图”(英语不是我的母语)。 - Sulthan
1
@UFO 这是暗淡视图。我相信它是纯黑色,alpha = 0.15。这真的很痛苦,特别是对于不被排除在暗淡之外的穿透视图。使用自定义的 popoverBackgroundViewClass 是唯一的解决方法。 - Sulthan
我还没有完成自定义的部分,但这是正确的答案。谢谢! - LE SANG
需要一个自定义的popoverBackgroundViewClass来防止全屏变暗似乎是很麻烦的(特别是当使用穿透视图时,变暗效果没有意义,并且默认情况下应该关闭)。我不怀疑它的工作原理,但难道没有其他更简单的方法吗? - Michael
1
很遗憾,没有其他方法可以做到这一点。请查看苹果开发者论坛,那里有关于此的长时间讨论。我相信他们想要模糊背景,但后来意识到如果不加暗淡效果,看起来并不好。 - Sulthan

2
另一种方法是遍历弹出视图堆栈并手动删除调暗视图,如UIPopoverController子类所示:
@property (nonatomic, assign) BOOL showsDimmingView;

....

- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item
           permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
                           animated:(BOOL)animated
{
    [super presentPopoverFromBarButtonItem:item
                  permittedArrowDirections:arrowDirections
                                  animated:animated];

    if (!_showsDimmingView) {
        [self removeDimmingView:[[UIApplication sharedApplication].keyWindow.subviews lastObject]];
    }
}

- (void)presentPopoverFromRect:(CGRect)rect
                        inView:(UIView *)view
      permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
                      animated:(BOOL)animated
{
    [super presentPopoverFromRect:rect
                           inView:view
         permittedArrowDirections:arrowDirections
                         animated:animated];

    if (!_showsDimmingView) {
        [self removeDimmingView:[[UIApplication sharedApplication].keyWindow.subviews lastObject]];
    }
}

- (void)removeDimmingView:(UIView *)subview
{
    for (UIView *sv in subview.subviews) {

        if (sv.alpha == 0.15f && [sv isKindOfClass:NSClassFromString(@"_UIPopoverViewBackgroundComponentView")]) {
            sv.alpha = 0.f;
        }

        const CGFloat *components = CGColorGetComponents(sv.backgroundColor.CGColor);
        if (sv.backgroundColor && (components[1] == 0.15f || sv.alpha == 0.15f)) {
            [sv removeFromSuperview];
        }

        [self removeDimmingView:sv];
    }
}

11
这种方法充满了危险。 - NSTJ
3
这种方法在iOS 8中已经不起作用了。你有另一个解决方案吗? - Alexandre Pepin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接