How to disable darker transparent effect in UIPopo

2019-06-17 18:39发布

I use UIPopoverController to popup an view in iPad iOS7 like this:

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

But when popover active, it make screen darker while this effect not affect other views in iOS6.

How to overcome this issue? Thanks!

2条回答
爷、活的狠高调
2楼-- · 2019-06-17 19:20

Another method is to traverse the popover view stack and remove the dimming view manually, as shown here in a UIPopoverController subclass:

@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];
    }
}
查看更多
疯言疯语
3楼-- · 2019-06-17 19:39

If you mean the dimming view that is inserted under the popover, there is only one workaround - use a custom popoverBackgroundViewClass.

It's complicated, but not as complicated as you might think.

查看更多
登录 后发表回答