简体   繁体   English

iOS:具有透明背景的Modal ViewController

[英]iOS: Modal ViewController with transparent background

I'm trying to present a view controller modally, with a transparent background. 我试图以模糊方式呈现视图控制器,具有透明背景。 My goal is to let both the presenting and presented view controllers's view to be displayed at the same time. 我的目标是让呈现和呈现的视图控制器视图同时显示。 The problem is, when the presenting animation finishes, the presenting view controller's view disappears. 问题是,当呈现动画结束时,呈现视图控制器的视图消失。

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

I know I could just add the view as a subview, but I'd like to avoid this solution for some reason. 我知道我可以将视图添加为子视图,但出于某种原因我想避免使用此解决方案。 How could I fix it? 我该怎么办呢?

For those trying to get this to work in iOS 8, the "Apple-approved" way to display a transparent modal view controller is by setting modalPresentationStyle on the present ed controller to UIModalPresentationOverCurrentContext . 对于那些试图让这个在iOS 8的工作,“苹果批准”的方式来显示一个透明的模态视图控制器是通过设置modalPresentationStyle 控制器上 UIModalPresentationOverCurrentContext

This can be done in code, or by setting the properties of the segue in the storyboard. 这可以在代码中完成,也可以通过在故事板中设置segue的属性来完成。

From the UIViewController documentation: 从UIViewController文档:

UIModalPresentationOverCurrentContext UIModalPresentationOverCurrentContext

A presentation style where the content is displayed over only the parent view controller's content. 一种演示样式,其中内容仅显示在父视图控制器的内容上。 The views beneath the presented content are not removed from the view hierarchy when the presentation finishes. 演示文稿完成后,不会从视图层次结构中删除所显示内容下方的视图。 So if the presented view controller does not fill the screen with opaque content, the underlying content shows through. 因此,如果呈现的视图控制器没有用不透明的内容填充屏幕,则底层内容会显示出来。

When presenting a view controller in a popover, this presentation style is supported only if the transition style is UIModalTransitionStyleCoverVertical. 在弹出框中显示视图控制器时,仅当过渡样式为UIModalTransitionStyleCoverVertical时才支持此演示样式。 Attempting to use a different transition style triggers an exception. 尝试使用不同的过渡样式会触发异常。 However, you may use other transition styles (except the partial curl transition) if the parent view controller is not in a popover. 但是,如果父视图控制器不在弹出框中,则可以使用其他过渡样式(部分卷曲过渡除外)。

Available in iOS 8.0 and later. 适用于iOS 8.0及更高版本。

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

The 'View Controller Advancements in iOS 8' video from WWDC 2014 goes into this in some detail. 来自WWDC 2014的“在iOS 8中查看控制器进展”视频详细介绍了这一点。

Note: 注意:

  • Be sure to give your presented view controller a clear background color, lest it not actually be see-through! 务必为您呈现的视图控制器提供清晰的背景颜色,以免它实际上是透明的!
  • You have to set this before presenting ie setting this parameter in the viewDidLoad of the presentedViewController won't have any affect 你必须呈现之前设置它即在viewDidLoad中设置此参数不会有任何影响

In iOS 8.0 and above it can be done by setting the property modalPresentationStyle to UIModalPresentationOverCurrentContext 在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];

请参见图像附加

This following code only works on the iPad. 以下代码仅适用于iPad。

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

I would go with adding a sub view. 我会添加一个子视图。

Here is a very good discussion. 这是一个非常好的讨论。 Look at the comments specifically. 具体看一下评论。 Not only the answer. 不仅是答案。

Modal View 模态视图

If I were you I wouldn't do it. 如果我是你,我就不会这样做。 I would add a sub view and do it. 我会添加一个子视图并执行它。 It seems to give me a better control over things. 它似乎让我更好地控制事物。

EDIT: 编辑:

As mentioned by Paul Linsay, since iOS 8 all that's needed is UIModalPresentationOverFullScreen for the modalPresentationStyle of the ViewController being presented. 正如Paul Linsay所提到的,因为iOS 8所需要的只是UIModalPresentationOverFullScreen用于呈现ViewController的modalPresentationStyle。 This would also cover of navigationBar and tabBar buttons. 这也包括navigationBar和tabBar按钮。

This code works fine on iPhone under iOS6 and iOS7: 此代码在iOS6和iOS7下的iPhone上运行正常:

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

In this case you miss slide-on animation. 在这种情况下,您会错过幻灯片动画。 To retain animation you still can use the following "non-elegant" extension: 要保留动画,您仍然可以使用以下“非优雅”扩展名:

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

If our presentingV is located inside of UINavigationController or UITabbarController you need to operate with that controllers as presentingVC. 如果我们的presentationV位于UINavigationController或UITabbarController内部,则需要使用该控制器作为presentationVC进行操作。

Further, in iOS7 you can implement custom transition animation applying UIViewControllerTransitioningDelegate protocol. 此外,在iOS7中,您可以使用UIViewControllerTransitioningDelegate协议实现自定义过渡动画。 Of course, in this case you can get transparent background 当然,在这种情况下,您可以获得透明背景

@interface ModalViewController : UIViewController <UIViewControllerTransitioningDelegate>

First, before presenting you have to set modalPresentationStyle 首先,在演示之前你必须设置modalPresentationStyle

modalViewController.modalPresentationStyle = UIModalPresentationCustom;

Then you have to implement two protocol methods 然后你必须实现两种协议方法

- (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;
}

The last thing is to define your custom transition in CustomAnimatedTransitioning class 最后一件事是在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
    }
}

I struggled a bit with the Interface Builder of XCode 7 to set the Presentation Style as @VenuGopalTewari suggested. 我使用XCode 7的Interface Builder稍微努力将Presentation Style设置为@VenuGopalTewari建议。 In this version, there seems to be no Over Current Context or Over Full Screen presentation mode for the segue. 在这个版本中,似乎没有针对segue的Over Current ContextOver Full Screen演示模式。 Thus, to make it work, I set the mode to Default : 因此,为了使其工作,我将模式设置为Default

在此输入图像描述 with 在此输入图像描述

Additionally I set the presentation mode of the modally presented view controller to Over Full Screen : 另外,我将模态呈现的视图控制器的演示模式设置为Over Full Screen

在此输入图像描述

Create a segue to present modally and set Presentation property of that segue to over current context it will work 100 % 创建一个segue以模态呈现并将该segue的Presentation属性设置为当前上下文它将100%工作

在此输入图像描述

PresentViewController with Transparent background - in iOS 8 and iOS 9 具有透明背景的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];

And in MYViewController set background color black and reduce opacity 并在MYViewController中设置背景颜色为黑色并降低不透明度

It's a bit of hacky way, but for me this code works (iOS 6): 这有点hacky方式,但对我来说这个代码有效(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;

    }];
}];

This code works also on iPhone 此代码也适用于iPhone

This category worked for me (ios 7, 8 and 9) 这个类别对我有用(ios 7,8和9)

H file H档

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

M file 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

If you're using Storyboard, you can follow this step: 如果您使用的是Storyboard,则可以按照以下步骤操作:

  1. Add a view controller (V2), setup the UI the way you want it 添加视图控制器(V2),按照您希望的方式设置UI
  • add an UIView - set background to black and opacity to 0.5 添加UIView - 将背景设置为黑色,将不透明度设置为0.5
  • add another UIView(2) - that will serve as your popup (Pls take note that the UIView and the UIView(2) must have the same level/hierarchy. Dont make the imageview the child of the view otherwise the opacity of the uiview will affect the UIView(2)) 添加另一个UIView(2) - 将作为你的弹出窗口(请注意UIView和UIView(2)必须具有相同的级别/层次结构。不要使imageview成为视图的子级别,否则uiview的不透明度将影响UIView(2))
  1. Present V2 Modally 礼物V2模态

  2. Click the segue. 单击segue。 In the Attributes inspector, Set Presentation as Over Full Screen . 在“属性”检查器中,将“演示文稿”设置为“全屏幕” Remove animation if you like 如果你愿意,删除动画

故事板

  1. Select V2. 选择V2。 In the Attributes inspector, Set Presentation as Over Full Screen . 在“属性”检查器中,将“演示文稿”设置为“全屏幕” Check Defines Context and Provides Context 检查定义上下文并提供上下文

故事板

  1. Select the MainView of your V2 (Pls. Check image). 选择V2的MainView(请检查图像)。 Set backgroundColor to Clear Color 将backgroundColor设置为Clear Color

故事板

I added these three lines in the init method in the presented view controller, and works like a charm: 我在呈现的视图控制器中的init方法中添加了这三行,并且像魅力一样工作:

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

EDIT (working on iOS 9.3): 编辑(在iOS 9.3上工作):

self.modalPresentationStyle = UIModalPresentationOverFullScreen;

As per documentation: 根据文件:

UIModalPresentationOverFullScreen A view presentation style in which the presented view covers the screen. UIModalPresentationOverFullScreen一种视图演示样式,其中呈现的视图覆盖屏幕。 The views beneath the presented content are not removed from the view hierarchy when the presentation finishes. 演示文稿完成后,不会从视图层次结构中删除所显示内容下方的视图。 So if the presented view controller does not fill the screen with opaque content, the underlying content shows through. 因此,如果呈现的视图控制器没有用不透明的内容填充屏幕,则底层内容会显示出来。

Available in iOS 8.0 and later. 适用于iOS 8.0及更高版本。

The solution to this answer using swift would be as follows. 使用swift解决这个问题的方法如下。

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

Alternate way is to use a "container view". 替代方法是使用“容器视图”。 Just make alpha below 1 and embed with seque. 只需将alpha设置为低于1并嵌入seque。 XCode 5, target iOS7. XCode 5,目标是iOS7。 Tested on iPhone. 在iPhone上测试过。

在此输入图像描述

Container view available from iOS6. 容器视图可从iOS6获得。 Link to blog post about that. 链接到博客文章。

I have created an object to handle the presentation of what I call a "superposed modal", meaning it retains the background's view and allows you to have a modal with a transparent background. 我创建了一个对象来处理我称之为“叠加模态”的表示,这意味着它保留了背景的视图,并允许您拥有一个透明背景的模态。

It has a single, simple method that does this: 它有一个简单的方法可以做到这一点:

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

It's important to set the modalPresentationCapturesStatusBarAppearance property to YES and force the status bar appearance to update, if your presented view controller has a different preferredStatusBarStyle . 如果您呈现的视图控制器具有不同的preferredStatusBarStyle ,则将modalPresentationCapturesStatusBarAppearance属性设置为YES并强制状态栏外观更新非常重要。

This object should have a @property (assign, nonatommic) isPresenting 该对象应具有@property (assign, nonatommic) isPresenting

You want this object to comply to the UIViewControllerAnimatedTransitioning and UIViewControllerTransitioningDelegate protocols and implement the following methods: 您希望此对象符合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;
}

and: 和:

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

This does a slide-in-from-the-bottom animation mimicking the default modal animation, but you can make it whatever you want. 这是一个模拟默认模态动画的底部幻灯片动画,但你可以随意制作它。

The important thing is that the presenting view controller's view will remain in the back, letting you create a transparent effect. 重要的是呈现视图控制器的视图将保留在后面,让您创建透明效果。

This solution works for iOS 7+ 此解决方案适用于iOS 7+

A very simple way of doing this (using Storyboards , for example) is: 一个非常简单的方法(例如使用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
}];

This will present a UIViewController in a Storyboard modally, but with a translucent background. 这将以模态方式在Storyboard呈现UIViewController ,但具有半透明背景。

Working for iOS 7-10 适用于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)
    }
}

To recap all the good answers and comments here and to still have an animation while moving to your new ViewController this is what I did: (Supports iOS 6 and up) 要在这里回顾所有好的答案和评论,并在移动到新的ViewController时仍然有动画,这就是我做的:(支持iOS 6及更高版本)

If your using a UINavigationController \\ UITabBarController this is the way to go: 如果你使用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];

If you'll do that you will lose your modalTransitionStyle animation. 如果你这样做,你将失去你的modalTransitionStyle动画。 In order to solve it you can easily add to your SomeViewController class this: 为了解决这个问题,您可以轻松地将您的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;
}

Of course you should set UIModalPresentationCurrentContext, but the place to set clearColor is very important too! 当然你应该设置UIModalPresentationCurrentContext,但设置clearColor的地方也很重要! You can't set background in viewDidLoad function, set it before the view did load like in the root view controller or in the init function of the controller that going to present! 你不能在viewDidLoad函数中设置背景,在视图加载之前设置它,就像在根视图控制器中或在将要出现的控制器的init函数中一样!

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

or 要么

- (instancetype)init {

    self = [super initWithNibName:nil bundle:nil];

    if(self) {
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [self.view setBackgroundColor:[UIColor clearColor]];
    }

    return self;
}

If you are using modal segue, make sure to set it as this image (you can turn off animation if you want) 如果您使用模态segue,请确保将其设置为此图像(如果需要,可以关闭动画) 在此输入图像描述

A complete method tested on iOS 7 and iOS 8. 在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

Swift 4.2 Swift 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)

in appdelegate : 在appdelegate:

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

in you first view controller from where you have to load next view: 在你第一次查看控制器,你必须从中加载下一个视图:

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

    }];

in your nextViewController which is to be added transparent: 在你的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];
}

The Login screen is a modal, meaning that it sits on top of the previous screen. “登录”屏幕是一个模态,表示它位于上一个屏幕的顶部。 So far we have Blurred Background, but it's not blurring anything; 到目前为止,我们有模糊背景,但它并没有模糊任何东西; it's just a grey background. 它只是一个灰色的背景。

We need to set our Modal properly. 我们需要正确设置我们的模态。

image link target 图像链接目标

  • First, we need to change the View Controller's View background to Clear color. 首先,我们需要将View Controller的View背景更改为Clear color。 It simply means that it should be transparent. 它只是意味着它应该是透明的。 By default, that View is white. 默认情况下,该视图为白色。

  • Second, we need to select the Segue that leads to the Login screen, and in the Attribute Inspector, set the Presentation to Over Current Context. 其次,我们需要选择通向Login屏幕的Segue,并在Attribute Inspector中将Presentation设置为Over Current Context。 This option is only available with Auto Layout and Size Classes enabled. 此选项仅在启用自动布局和大小类时可用。

image link target 图像链接目标

Set navigation's modalPresentationStyle to UIModalPresentationCustom 将导航的modalPresentationStyle设置为UIModalPresentationCustom

and set your presented view controller's background color as clear color. 并将您呈现的视图控制器的背景颜色设置为清晰的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM