如何将灰色背景添加到自定义UIPopoverBackgroundView

sas*_*ash 5 objective-c uipopovercontroller uipopoverbackgroundview

我通过继承UIPopoverBackgroundView(使用本教程)并使用UIPopoverController呈现它来制作自定义弹出窗口.不幸的是,只要我指定自定义popoverBackgroundViewClass,原生的灰色背景就会消失.使用自定义UIPopoverBackgroundView时,有没有办法让灰色背景变暗?我可以用来模拟本机行为的任何其他解决方案?

Aar*_*egh 5

不知道为什么这个被投票,这是一个很好的问题,因为当你实现自定义UIPopoverBackgroundView时,暗灰色的背景不会被设置.在研究这个问题时,我确定最好的方法是自己设置它!

在创建弹出窗口视图之前,我创建了一个"蒙版视图",它将在弹出窗口之前添加到视图中.此代码还包含一个很好的淡入淡出效果:

self.customPopoverMaskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.customPopoverMaskView.backgroundColor = [UIColor blackColor];
    self.customPopoverMaskView.alpha = 0.3f;

    [UIView transitionWithView:self.view
                      duration:0.3
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^ {
                        [self.view addSubview:self.customPopoverMaskView];
                    }
                    completion:nil];
Run Code Online (Sandbox Code Playgroud)

要删除视图,请将其插入处理弹出视图消失的方法:

[UIView transitionWithView:self.view
                  duration:0.3
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^ {
                    [self.customPopoverMaskView removeFromSuperview];
                }
                completion:nil];
Run Code Online (Sandbox Code Playgroud)

对我来说效果很好.快乐的编码!

亚伦


Dmi*_*zov 5

您只需将下一个代码添加到initWithFrame:UIPopoverBackgroundView实现的方法中.

UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0 - self.frame.origin.x,
                                                           0 - self.frame.origin.y,
                                                           [UIScreen mainScreen].bounds.size.width,
                                                           [UIScreen mainScreen].bounds.size.height)];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0.15;
dimView.userInteractionEnabled = NO;
[self addSubview:dimView];
Run Code Online (Sandbox Code Playgroud)

它与默认的Apple实现相同.