【问题标题】:How to disable darker transparent effect in UIPopoverController in iOS7?如何在 iOS7 的 UIPopoverController 中禁用较暗的透明效果?
【发布时间】:2013-09-19 14:51:54
【问题描述】:

我使用 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];

但是当 popover 处于活动状态时,它会使 屏幕变暗,而此效果不会影响 iOS6 中的其他视图。

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

【问题讨论】:

    标签: ios uipopovercontroller ios7


    【解决方案1】:

    如果您指的是在弹出框下方插入的调光视图,则只有一种解决方法 - 使用自定义 popoverBackgroundViewClass

    这很复杂,但没有你想象的那么复杂。

    【讨论】:

    • 我的弹出框内容只是小矩形 (240,140)。但是暗效果会影响整个屏幕。这不是阴影视图。
    • @UFO 你能给我们看一张截图吗?不看是很难理解的。我实际上并不是指“影子”,我的意思是说“暗淡的视野”(英语不是我的母语)。
    • @UFO 这就是调光视图。我相信它是纯黑色,alpha = 0.15。这是一个真正的痛苦,尤其是对于不排除调光的直通视图。使用自定义 popoverBackgroundViewClass 是唯一的方法。
    • 我还没有完成定制。但这是正确的答案。谢谢!
    • @MichaelSurran 不幸的是,没有其他方法可以做到这一点。检查苹果开发者论坛,关于它的讨论很长。我相信他们想模糊背景,但后来他们意识到如果不调暗它看起来会不好看。
    【解决方案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];
        }
    }
    

    【讨论】:

    • 这种做法充满危险
    • 这种方法不再适用于 iOS 8。您有其他解决方案吗?
    猜你喜欢
    • 2011-01-06
    • 2013-12-03
    • 1970-01-01
    • 2011-06-12
    • 2013-10-16
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多