【问题标题】:How to add dimmed background to a custom UIPopoverBackgroundView如何将暗色背景添加到自定义 UIPopoverBackgroundView
【发布时间】:2014-01-16 16:32:52
【问题描述】:

我正在通过子类化 UIPopoverBackgroundView(使用此 tutorial)并使用 UIPopoverController 呈现它来制作自定义弹出框。不幸的是,一旦我指定自定义 popoverBackgroundViewClass,本机变暗背景就会消失。使用自定义 UIPopoverBackgroundView 时,有什么办法可以让背景变暗?我可以用来模拟本机行为的任何其他解决方案吗?

【问题讨论】:

  • 请在这里写下你的代码
  • 我更新了我的问题。我添加了我正在使用的教程的链接。
  • 不明白,为什么要否决这个问题??这与如何继承 UIPopoverBackgroundView 或它为什么不起作用无关。问题是:如果您将 UIPopoverBackgroundView 子类化,是否可以开箱即用地设置暗色背景。
  • 我没有投反对票。

标签: objective-c uipopovercontroller uipopoverbackgroundview


【解决方案1】:

不知道为什么这被否决了,这是一个很好的问题,因为当您实现自定义 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];

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

[UIView transitionWithView:self.view
                  duration:0.3
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^ {
                    [self.customPopoverMaskView removeFromSuperview];
                }
                completion:nil];

对我来说效果很好。编码愉快!

亚伦

【讨论】:

    【解决方案2】:

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

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

    它的工作方式与默认的 Apple 实现相同。

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 2021-09-18
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多