Create a black transparent modal controller

I wanted to create a modal view controller with a black background, but I want to make alpha 0.9, so that I still see the view behind it partially. How can I do it? I know that I can use UIView and then just add this as a preview, however, if I have a UINavigationController or UITabBarController, this will not cover the whole screen. Looking for some suggestions on this subject, as far as I know, the solutions that I have seen so far have never dealt with a colored background. Most want only a transparent background.

+4
source share
4 answers

This one will help you ...

You just need to set the background color according to your requirement.

+6
source

You tried

[viewController1 presentModalViewController:viewController2 animated:YES]; 

I have not tried it myself, but the documentation says that it always presents it in full screen. Then just set your view to backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.9]; . (white = 0.0 black). Add any subheadings you want. Or set the background color to full black, and the alpha of the whole view is 0.9. It depends on whether you want the subviews to be a bit translucent.

0
source

I got this idea from https://gist.github.com/1279713

Preparation: In the xib modal view (or scene using the storyboard), I set up a full-screen UIImageView background (connect it to the .h file and give it the backgroundImageView property) with 0.3 alpha. And I set the background color of the view (UIView) as plain black.

Idea: Then, in the "viewDidLoad" of the modal view controller, I capture a screenshot from the initial state and set this image to the background of the UIImageView. Set the starting point Y to -480 and let it move to point Y 0 using a 0.4 second duration with the EaseInOut animation option. When we reject the view manager, just do the opposite.

Code for Modal View Controller Class

.h file:

 @property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView; - (void) backgroundInitialize; - (void) backgroundAnimateIn; - (void) backgroundAnimateOut; 

.m file:

 - (void) backgroundInitialize{ UIGraphicsBeginImageContextWithOptions(((UIViewController *)delegate).view.window.frame.size, YES, 0.0); [((UIViewController *)delegate).view.window.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); backgroundImageView.image=screenshot; } - (void) backgroundAnimateIn{ CGRect backgroundImageViewRect = backgroundImageView.frame; CGRect backgroundImageViewRectTemp = backgroundImageViewRect; backgroundImageViewRectTemp.origin.y=-480; backgroundImageView.frame=backgroundImageViewRectTemp; [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^{ backgroundImageView.frame=backgroundImageViewRect; } completion:^(BOOL finished) { }]; } - (void) backgroundAnimateOut{ CGRect backgroundImageViewRect = backgroundImageView.frame; backgroundImageViewRect.origin.y-=480; [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^{ backgroundImageView.frame=backgroundImageViewRect; } completion:^(BOOL finished) { }]; } 

In viewDidLoad just call:

 [self backgroundInitialize]; [self backgroundAnimateIn]; 

At any place where we reject the modal view controller, we call:

 [self backgroundAnimateOut]; 

Please note that this will ALWAYS animate the background image. Therefore, if this modal presentation transition style (or segue transition style) is not set to "Cover Vertical", you may not need to invoke animation methods.

0
source

First add a UIImageView to the .h file

 UIImageView *overLayImage; 

now add below code to .m file in viewDidLoad ()

 overLayImage=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"overlay1px.png"]] ; overLayImage.frame = self.view.frame; overLayImage.frame = CGRectMake(0, 0, 1024, 748); 

Where "overlay1px.png" is a transparent image of your color choice with a height and width of 1px X 1px.

Now in your IBAction, where do you want to add your view with transparent backgroud from, add the code below

 [self.view addSubview:overLayImage]; [vwTermsCondition setFrame:CGRectMake(262, 300, 500, 120)]; [vwTermsCondition.layer setCornerRadius:5.0f]; [vwTermsCondition.layer setShadowColor:[UIColor blackColor]]; [vwTermsCondition.layer setMasksToBounds:YES]; [self.view addSubview:vwTermsCondition]; 

In the above code, vwTermsCondtion is the name of your view. This is for iPad applications you can adjust the height / width for iPhone.

Enjoy :)

-1
source

All Articles