【问题标题】:iOS: Modal ViewController with transparent backgroundiOS:具有透明背景的模态 ViewController
【发布时间】:2012-10-05 07:04:49
【问题描述】:

我正在尝试以模态方式呈现具有透明背景的视图控制器。我的目标是让呈现和呈现的视图控制器的视图同时显示。问题是,当呈现动画结束时,呈现视图控制器的视图消失了。

- (IBAction)pushModalViewControllerButtonPressed:(id)sender
{
    ModalViewController *modalVC = [[ModalViewController alloc] init];
    [self presentViewController:modalVC animated:YES completion:nil];
}

我知道我可以将视图添加为子视图,但出于某种原因我想避免使用此解决方案。我该如何解决?

【问题讨论】:

  • @TheKing 听起来像迈克尔和我一样,希望模态视图是半透明的,在主要(呈现)视图上显示为悬停的凝胶层。在进行快速设置的同时创造用户停留在当前环境中的感觉,而不是去使用其他一些主要功能(在单独的视图中)。
  • 这个stackoverflow.com/q/27598846/1603234让我微笑,现在轮到你了:)
  • 在这里查看我的答案stackoverflow.com/a/29794201/1606125 对于 iOS 8
  • 对于 iOS 10 正在使用 UIModalPresentationOverFullScreen 作为 inigo333 如下所述。

标签: ios objective-c modalviewcontroller presentmodalviewcontroller


【解决方案1】:

对于那些试图让它在 iOS 8 中工作的人来说,“Apple 认可”的显示透明模式视图控制器的方法是在当前ed 控制器上设置 modalPresentationStyle UIModalPresentationOverCurrentContext

这可以在代码中完成,也可以通过在故事板中设置 segue 的属性来完成。

来自 UIViewController 文档:

UIModalPresentationOverCurrentContext

内容仅显示在 父视图控制器的内容。所呈现的视图下方 演示时内容不会从视图层次结构中删除 完成。所以如果呈现的视图控制器没有填满屏幕 对于不透明的内容,底层内容会显示出来。

在弹出窗口中显示视图控制器时,此显示 仅当过渡样式为 UIModalTransitionStyleCoverVertical。尝试使用不同的 过渡风格触发异常。但是,您可以使用其他 过渡样式(部分卷曲过渡除外)如果父 视图控制器不在弹出窗口中。

适用于 iOS 8.0 及更高版本。

https://developer.apple.com/documentation/uikit/uiviewcontroller

WWDC 2014 的“iOS 8 中的视图控制器改进”视频对此进行了详细介绍。

注意:

  • 确保为您呈现的视图控制器提供清晰的背景颜色,以免它实际上不透明!
  • 你必须设置这个之前呈现即在presentedViewController的viewDidLoad中设置这个参数不会有任何影响

【讨论】:

  • 注意 - 您需要设置 modalPresentationStyle 的目标似乎已更改。例如,在 iOS 7 中,要使其正常工作,我需要将试图打开模式的视图控制器的 modalPresentationStyle 设置为 UIModalPresentationCurrentContext。但是,要让它在 iOS8 中工作,我需要将要显示的模态视图的 modalPresentationStyle 设置为 UIModalPresentationOverCurrentContext
  • 如果在故事板中使用 segues,您必须在那里设置演示样式。至少这对我有用。
  • 我在呈现的视图控制器的 init 方法中添加了这三行,就像一个魅力:self.providesPresentationContextTransitionStyle = YES; self.definesPresentationContext = YES; [self setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  • 在 iOS 8+ 上,我必须从 UINavigationController 展示一个模式,所以从一个孩子那里展示并不能提供我需要的东西。相反,sourceVCself.navigationController。另外,只有将目标演示样式设置为自定义后,我才能看穿它。 [sourceVC setModalPresentationStyle:UIModalPresentationCurrentContext];, [targetVC setModalPresentationStyle:UIModalPresentationCustom]; 希望它对某人有所帮助。
  • 注意 - 在 presenting VC 中致电 presentedVC.modalPresentationStyle = UIModalPresentationOverCurrentContext。在presentedVC中不起作用。相信我,我试过了。
【解决方案2】:

在 iOS 8.0 及更高版本中,可以通过将属性 modalPresentationStyle 设置为 UIModalPresentationOverCurrentContext

来完成
//Set property **definesPresentationContext** YES to avoid presenting over presenting-viewController's navigation bar

self.definesPresentationContext = YES; //self is presenting view controller
presentedController.view.backgroundColor = [YOUR_COLOR with alpha OR clearColor]
presentedController.modalPresentationStyle = UIModalPresentationOverCurrentContext;

[self presentViewController:presentedController animated:YES completion:nil];

【讨论】:

  • 这是我可以开始工作的唯一解决方案。也很简单。只需在模态 segue 之前将其添加到您正在展示的 VC 中。
  • Xcoe 9.2/ iOS 11.2 swift .custom.overFullScreen 工作。
  • 如果你设置.overFullScreen,当前视图控制器的viewWillAppear 似乎不会被调用。
  • 还有一点,presentedController.view.backgroundColor = #color#应该写成presentedControllerviewDidLoad,否则presentedController的生活就被打断了。
  • Xcode 12.2 和 iOS 14.2 将于 2021 年加入此主题,这是唯一有效的解决方案。
【解决方案3】:

以下代码仅适用于 iPad。

self.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:modalVC animated:YES];

我会添加一个子视图。

这是一个很好的讨论。具体看cmets。不仅仅是答案。

Modal View

如果我是你,我不会这样做。我会添加一个子视图并执行此操作。这似乎让我可以更好地控制事情。

编辑:

正如 Paul Linsay 所提到的,从 iOS 8 开始,所需要的只是 UIModalPresentationOverFullScreen 来呈现 ViewController 的 modalPresentationStyle。这也包括 navigationBar 和 tabBar 按钮。

【讨论】:

  • 确实适用于 iOS 7 和 iPhone,问题是您必须在 presenter 视图控制器上指定 modalPresentationStyle = UIModalPresentationCurrentContext ,而不是在呈现的中。
  • 是的,它有效,但仅当您将其设置在视图层次结构的 更高 ViewController 时。 (例如,NavigationController 或幻灯片菜单视图控制器的实例)。
  • 奇怪的是,在我的情况下,它仅在将模态样式设置为 UIModalPresentationCustom 后才有效,并且仅当它设置在 presentViewController 之前而不是在 viewDidLoad 之前。
  • 从 iOS 8 开始,所需要的只是 UIModalPresentationOverFullScreen 用于呈现的 ViewController 的 modalPresentationStyle。
  • 尝试了大约 1000 次后,我意识到您需要设置modalPresentationStyle viewDidLoad 之前。我在构造函数中做了它并且它有效。
【解决方案4】:

此代码在 iOS6 和 iOS7 下的 iPhone 上运行良好:

presentedVC.view.backgroundColor = YOUR_COLOR; // can be with 'alpha'
presentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentingVC presentViewController:presentedVC animated:YES completion:NULL];

在这种情况下,您会错过滑入式动画。要保留动画,您仍然可以使用以下“非优雅”扩展:

[presentingVC presentViewController:presentedVC animated:YES completion:^{
    [presentedVC dismissViewControllerAnimated:NO completion:^{
        presentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
        [presentingVC presentViewController:presentedVC animated:NO completion:NULL];
    }];
}];

如果我们的presentingV 位于UINavigationController 或UITabbarController 内部,您需要将这些控制器作为presentingVC 进行操作。

此外,在 iOS7 中,您可以应用UIViewControllerTransitioningDelegate 协议实现自定义过渡动画。当然,这种情况下可以得到透明背景

@interface ModalViewController : UIViewController <UIViewControllerTransitioningDelegate>

首先,在演示之前你必须设置modalPresentationStyle

modalViewController.modalPresentationStyle = UIModalPresentationCustom;

那你得实现两个协议方法

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    CustomAnimatedTransitioning *transitioning = [CustomAnimatedTransitioning new];
    transitioning.presenting = YES;
    return transitioning;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    CustomAnimatedTransitioning * transitioning = [CustomAnimatedTransitioning new];
    transitioning.presenting = NO;
    return transitioning;
}

最后一件事是在CustomAnimatedTransitioning 类中定义您的自定义过渡

@interface CustomAnimatedTransitioning : NSObject <UIViewControllerAnimatedTransitioning>
@property (nonatomic) BOOL presenting;
@end

@implementation CurrentContextTransitionAnimator

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext 
{
    return 0.25;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext 
{
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    if (self.presenting) {
        // custom presenting animation
    }
    else {
        // custom dismissing animation
    }
}

【讨论】:

  • 确认这个作品。 注意 如果您的 viewController 位于 navigationController 中,请确保其 modalPresentationStyle 也已设置!
  • 我认为在最常见的情况下,最好将导航控制器视为正在呈现VC。
  • 是的,使用这个modalPresentationStyle不可能得到呈现动画((
  • @alex 是的,很抱歉:我的错误是将 modalPresentationStyle 设置为 UIModalPresentationCurrentContext 用于presentedVC而不是prenstingVC。
  • @mxcl 如果您的 viewController 的 navigationController 本身位于 tabBarController 中,那么请确保也设置了它的 modalPresentationStyle ;)
【解决方案5】:

我在 XCode 7 的 Interface Builder 上有点挣扎,无法按照 @VenuGopalTewari 的建议设置 Presentation Style。在这个版本中,segue 似乎没有Over Current ContextOver Full Screen 演示模式。因此,为了让它工作,我将模式设置为Default

另外我将模态显示的视图控制器的显示模式设置为Over Full Screen

【讨论】:

  • 这对我有用,不需要代码。我正在使用 Xcode 7.2,swift,目标是 iOS 8.0
  • 这对我也有用。 +1 为正确答案。谢谢:)
  • 最佳答案在这里!
  • 无代码 = 最佳答案。巴斯蒂安赢得了这场比赛。请大家 +1 这个答案。
  • 这个答案有效..花了一个多小时
【解决方案6】:

创建一个 segue 以模态方式呈现并将该 segue 的 Presentation 属性设置为当前上下文 它将工作 100%

【讨论】:

  • 我很佩服他们 100% 的肯定,它奏效了!关键是将其应用于segue。非常感谢
  • 如果您没有将 segue 连接到按钮并且以编程方式调用它,请确保设置标识符并调用 performSegueWithIdentifier 而不是 presentViewController
  • 我尝试了所有其他答案,但这是 iOS9 中唯一对我有用的答案。
【解决方案7】:

使用 swift 解决这个问题的方法如下。

let vc = MyViewController()
vc.view.backgroundColor = UIColor.clear // or whatever color.
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)

【讨论】:

    【解决方案8】:

    带有透明背景的PresentViewController - 在iOS 8 和iOS 9 中

    MYViewController *myVC = [self.storyboard   instantiateViewControllerWithIdentifier:@"MYViewController"];
        myVC.providesPresentationContextTransitionStyle = YES;
        myVC.definesPresentationContext = YES;
        [myVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];
        [self.navigationController presentViewController:myVC animated:YES completion:nil];
    

    并在 MYViewController 中设置背景颜色为黑色并降低不透明度

    【讨论】:

      【解决方案9】:

      这有点老套,但对我来说,这段代码有效(iOS 6):

      AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
      
      [self presentViewController:self.signInViewController animated:YES completion:^{
          [self.signInViewController dismissViewControllerAnimated:NO completion:^{
              appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
              [self presentViewController:self.signInViewController animated:NO completion:nil];
              appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;
      
          }];
      }];
      

      此代码也适用于 iPhone

      【讨论】:

      • 谢谢 - 这正是我所需要的 - 在主主页顶部显示初始“帮助”屏幕。漂亮!
      • 今天,我花了将近 3 个小时浏览和搜索适用于 iOS7 和 iOS8 的解决方案。考虑到这个答案已有 1.5 年历史并且我正在使用 ECSlidingViewController,这是唯一有效的解决方案!谢谢@Mak。
      • 完美。另外,如果您不关心动画,则内部完成中的 3 行完美地工作 谢谢!
      • 花了 3 个小时没有任何成功后发现这个,干杯,希望我能投票更多:D
      【解决方案10】:

      这个类别对我有用(ios 7、8 和 9)

      H 文件

      @interface UIViewController (navigation)
      - (void) presentTransparentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;
      @end
      

      M 文件

      @implementation UIViewController (navigation)
      - (void)presentTransparentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
      {
          if(SYSTEM_VERSION_LESS_THAN(@"8.0")) {
              [self presentIOS7TransparentController:viewControllerToPresent withCompletion:completion];
      
          }else{
              viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverCurrentContext;
               [self presentViewController:viewControllerToPresent animated:YES completion:completion];
          }
      }
      -(void)presentIOS7TransparentController:(UIViewController *)viewControllerToPresent withCompletion:(void(^)(void))completion
      {
          UIViewController *presentingVC = self;
          UIViewController *root = self;
          while (root.parentViewController) {
              root = root.parentViewController;
          }
          UIModalPresentationStyle orginalStyle = root.modalPresentationStyle;
          root.modalPresentationStyle = UIModalPresentationCurrentContext;
          [presentingVC presentViewController:viewControllerToPresent animated:YES completion:^{
              root.modalPresentationStyle = orginalStyle;
          }];
      }
      @end
      

      【讨论】:

      • 我检查了所有的答案.. 但对我来说,你的答案是完美的工作.. 为此“+1”。 @泰德
      • 我以为我找不到解决方案。非常感谢!
      【解决方案11】:

      如果您使用 Storyboard,则可以按照以下步骤操作:

      1. 添加视图控制器 (V2),按照您想要的方式设置 UI
      • 添加 UIView - 将背景设置为黑色,不透明度设置为 0.5
      • 添加另一个 UIView(2) - 这将作为您的弹出窗口(请注意 UIView 和 UIView(2) 必须具有相同的级别/层次结构。不要将 imageview 设置为视图的子视图,否则会导致uiview 会影响 UIView(2))
      1. 以模态方式呈现 V2

      2. 单击转场。在属性检查器中,将 Presentation 设置为 Over Full Screen。如果喜欢,请删除动画

      1. 选择 V2。在属性检查器中,将 Presentation 设置为 Over Full Screen。检查定义上下文并提供上下文

      1. 选择您的 V2 的 MainView(请检查图像)。将 backgroundColor 设置为 清除颜色

      【讨论】:

        【解决方案12】:

        我在呈现的视图控制器的 init 方法中添加了这三行代码,效果非常好:

        self.providesPresentationContextTransitionStyle = YES;
        self.definesPresentationContext = YES;
        [self setModalPresentationStyle:UIModalPresentationOverCurrentContext];
        

        编辑(在 iOS 9.3 上工作):

        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
        

        根据文档:

        UIModalPresentationOverFullScreen 呈现的视图覆盖屏幕的视图呈现样式。当演示结束时,演示内容下方的视图不会从视图层次结构中删除。因此,如果呈现的视图控制器没有用不透明的内容填充屏幕,则底层内容会显示出来。

        适用于 iOS 8.0 及更高版本。

        【讨论】:

        • 在 iOS 10.2 上工作。
        【解决方案13】:

        另一种方法是使用“容器视图”。只需使 alpha 低于 1 并嵌入序列。 XCode 5,针对 iOS7。在 iPhone 上测试。

        可从 iOS6 获得容器视图。 Link 发表关于此的博文。

        【讨论】:

          【解决方案14】:

          我创建了一个对象来处理我所谓的“叠加模式”的呈现,这意味着它保留了背景的视图并允许您拥有一个具有透明背景的模式。

          它有一个简单的方法可以做到这一点:

          - (void)presentViewController:(UIViewController *)presentedViewController
                 fromViewController:(UIViewController *)presentingViewController
          {
              presentedViewController.modalPresentationStyle = UIModalPresentationCustom;
              presentedViewController.transitioningDelegate = self;
              presentedViewController.modalPresentationCapturesStatusBarAppearance = YES;
          
              [presentedViewController setNeedsStatusBarAppearanceUpdate];
          
              [presentingViewController presentViewController:presentedViewController
                                                 animated:YES
                                               completion:nil];
          }
          

          如果您呈现的视图控制器具有不同的preferredStatusBarStyle,请将modalPresentationCapturesStatusBarAppearance 属性设置为YES 并强制更新状态栏外观非常重要。

          这个对象应该有一个@property (assign, nonatommic) isPresenting

          您希望此对象符合UIViewControllerAnimatedTransitioningUIViewControllerTransitioningDelegate 协议并实现以下方法:

          - (id)animationControllerForPresentedController:(UIViewController *)presented
                                     presentingController:(UIViewController *)presenting
                                         sourceController:(UIViewController *)source
          {
              self.isPresenting = YES;
          
              return self;
          }
          
          - (id)animationControllerForDismissedController:(UIViewController *)dismissed
          {
              self.isPresenting = NO;
          
              return self;
          }
          

          和:

          - (NSTimeInterval)transitionDuration:(id)transitionContext
          {
              return 0.25;
          }
          
          - (void)animateTransition:(id)transitionContext
          {
              UIViewController* firstVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
              UIViewController* secondVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
              UIView* containerView = [transitionContext containerView];
              UIView* firstView = firstVC.view;
              UIView* secondView = secondVC.view;
          
              if (self.isPresenting) {
                  [containerView addSubview:secondView];
                  secondView.frame = (CGRect){
                      containerView.frame.origin.x,
                      containerView.frame.origin.y + containerView.frame.size.height,
                      containerView.frame.size
                  };
          
                  firstView.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
                  [UIView animateWithDuration:0.25 animations:^{
                      secondView.frame = containerView.frame;
                  } completion:^(BOOL finished) {
                      [transitionContext completeTransition:YES];
                  }];
                  } else {
                  [UIView animateWithDuration:0.25 animations:^{
                      firstView.frame = (CGRect){
                          containerView.frame.origin.x,
                          containerView.frame.origin.y + containerView.frame.size.height,
                          containerView.frame.size
                  };
          
                  } completion:^(BOOL finished) {
                      [transitionContext completeTransition:YES];
                  }];
              }
          }
          

          这是一个从底部滑入的动画,模仿默认的模态动画,但您可以随意制作。

          重要的是呈现视图控制器的视图将保留在后面,让您创建透明效果。

          此解决方案适用于 iOS 7+

          【讨论】:

          • 感谢您的回答。
          【解决方案15】:

          一个非常简单的方法(例如使用Storyboards)是:

          UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SomeStoryboard" bundle:nil];
          UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SomeStoryboardViewController"];
          // the key for what you're looking to do:
          vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
          vc.view.alpha = 0.50f;
          
          [self presentViewController:vc animated:YES completion:^{
              // great success
          }];
          

          这将以模态方式在Storyboard 中显示UIViewController,但背景为半透明。

          【讨论】:

            【解决方案16】:

            适用于 iOS 7-10

            if #available(iOS 8.0, *) {
                nextVC.modalPresentationStyle = .OverCurrentContext
                self.presentViewController(nextVC, animated: true, completion: nil)
            } else {
                // Fallback on earlier version
                self.modalPresentationStyle = .Custom          
                nextVC.modalTransitionStyle = .CrossDissolve            
                self.presentViewController(nextVC, animated: false, completion: nil)
                }
            }
            

            【讨论】:

              【解决方案17】:

              在这里回顾所有好的答案和 cmets 并在移动到您的新 ViewController 时仍然有动画,这就是我所做的:(支持 iOS 6 及更高版本)

              如果您使用UINavigationController \ UITabBarController,这就是要走的路:

                  SomeViewController *vcThatWillBeDisplayed = [self.storyboard instantiateViewControllerWithIdentifier:@"SomeVC"];
              
                  vcThatWillBeDisplayed.view.backgroundColor = [UIColor colorWithRed: 255/255.0 green:255/255.0 blue:255/255.0 alpha:0.50];    
              
                  self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
                  [self presentViewController:presentedVC animated:YES completion:NULL];
              

              如果你这样做,你将失去你的modalTransitionStyle 动画。为了解决它,您可以轻松地将以下内容添加到您的 SomeViewController 类中:

              -(void)viewDidAppear:(BOOL)animated
              {
                  [super viewDidAppear:animated];
                  [UIView animateWithDuration:0.4 animations:^() {self.view.alpha = 1;}
                     completion:^(BOOL finished){}];
              }
              - (void)viewDidLoad
              {
                  [super viewDidLoad];
                  self.view.alpha = 0;
              }
              

              【讨论】:

                【解决方案18】:

                当然要设置UIModalPresentationCurrentContext,但是设置clearColor的地方也很重要!您不能在 viewDidLoad 函数中设置背景,在视图加载之前设置它,就像在 根视图控制器 或将要呈现的控制器的 init 函数 中一样!

                actionController.view.backgroundColor = [UIColor clearColor];
                [self presentViewController:actionController animated:YES completion:nil];
                

                - (instancetype)init {
                
                    self = [super initWithNibName:nil bundle:nil];
                
                    if(self) {
                        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
                        [self.view setBackgroundColor:[UIColor clearColor]];
                    }
                
                    return self;
                }
                

                【讨论】:

                • 这是帮助我的提示。感谢您的帮助。
                【解决方案19】:

                如果你使用的是模态segue,请确保将其设置为这个图像(如果你愿意,你可以关闭动画)

                【讨论】:

                  【解决方案20】:

                  在 iOS 7 和 iOS 8 上测试的完整方法。

                  @interface UIViewController (MBOverCurrentContextModalPresenting)
                  
                  /// @warning Some method of viewControllerToPresent will called twice before iOS 8, e.g. viewWillAppear:.
                  - (void)MBOverCurrentContextPresentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;
                  
                  @end
                  
                  @implementation UIViewController (MBOverCurrentContextModalPresenting)
                  
                  - (void)MBOverCurrentContextPresentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
                      UIViewController *presentingVC = self;
                  
                      // iOS 8 before
                      if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
                          UIViewController *root = presentingVC;
                          while (root.parentViewController) {
                              root = root.parentViewController;
                          }
                  
                          [presentingVC presentViewController:viewControllerToPresent animated:YES completion:^{
                              [viewControllerToPresent dismissViewControllerAnimated:NO completion:^{
                                  UIModalPresentationStyle orginalStyle = root.modalPresentationStyle;
                                  if (orginalStyle != UIModalPresentationCurrentContext) {
                                      root.modalPresentationStyle = UIModalPresentationCurrentContext;
                                  }
                                  [presentingVC presentViewController:viewControllerToPresent animated:NO completion:completion];
                                  if (orginalStyle != UIModalPresentationCurrentContext) {
                                      root.modalPresentationStyle = orginalStyle;
                                  }
                              }];
                          }];
                          return;
                      }
                  
                      UIModalPresentationStyle orginalStyle = viewControllerToPresent.modalPresentationStyle;
                      if (orginalStyle != UIModalPresentationOverCurrentContext) {
                          viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverCurrentContext;
                      }
                      [presentingVC presentViewController:viewControllerToPresent animated:YES completion:completion];
                      if (orginalStyle != UIModalPresentationOverCurrentContext) {
                          viewControllerToPresent.modalPresentationStyle = orginalStyle;
                      }
                  }
                  
                  @end
                  

                  【讨论】:

                    【解决方案21】:

                    斯威夫特 4.2

                    guard let someVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "someVC") as? someVC else {
                        return
                    }
                    someVC.modalPresentationStyle = .overCurrentContext
                    
                    present(someVC, animated: true, completion: nil)
                    

                    【讨论】:

                      【解决方案22】:

                      在 appdelegate 中:

                      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
                      {
                          [[_window rootViewController]setModalPresentationStyle:UIModalPresentationCurrentContext];
                          return YES;
                      }
                      

                      在您必须加载下一个视图的第一个视图控制器中:

                        NextViewController *customvc = [[NextViewController alloc]init];
                          [self presentViewController:customvc animated:YES completion:^{
                      
                          }];
                      

                      在要添加透明的 nextViewController 中:

                      - (void)viewDidLoad
                      {
                          [super viewDidLoad];
                          self.view.backgroundColor = [UIColor clearColor];
                          UIView* backView = [[UIView alloc] initWithFrame:self.view.frame];
                          backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
                          [self.view insertSubview:backView atIndex:0];
                      }
                      

                      【讨论】:

                        【解决方案23】:

                        登录屏幕是一个模式,这意味着它位于前一个屏幕的顶部。到目前为止,我们已经模糊了背景,但它并没有模糊任何东西;它只是一个灰色的背景。

                        我们需要正确设置我们的 Modal。

                        image link target

                        • 首先,我们需要将 View Controller 的 View 背景更改为 Clear color。它只是意味着它应该是透明的。默认情况下,该视图是白色的。

                        • 其次,我们需要选择通向登录屏幕的 Segue,并在 Attribute Inspector 中,将 Presentation 设置为 Over Current Context。此选项仅在启用自动布局和大小类的情况下可用。

                        image link target

                        【讨论】:

                          【解决方案24】:

                          将导航的modalPresentationStyle 设置为UIModalPresentationCustom

                          并将您呈现的视图控制器的背景颜色设置为清晰的颜色。

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 2014-08-12
                            • 2014-12-23
                            • 2016-05-01
                            • 1970-01-01
                            相关资源
                            最近更新 更多