Presenting a view controller with dim background
Solution 1
Is this what you're looking for?
View Controller A -> View Controller B (nib)
Swift < 3:
In View Controller A, add the following line of code:
let viewControllerB = ViewControllerB(nibName: "ViewControllerB", bundle: nil)
viewControllerB.modalPresentationStyle = .OverFullScreen
presentViewController(viewControllerB, animated: true, completion: nil)
And in View Controller B, set the background color of view
with colorWithAlphaComponent
method:
view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
Swift ≥ 3:
View Controller A:
let viewControllerB = ViewControllerB(nibName: "ViewControllerB", bundle: nil)
viewControllerB.modalPresentationStyle = .overFullScreen
present(viewControllerB, animated: true, completion: nil)
View Controller B:
view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
Solution 2
Swift 3.0 Code
let addIncidentViewController = self.storyboard?.instantiateViewController(withIdentifier: "addIncidentViewController") as! AddIncidentViewController
addIncidentViewController.modalPresentationStyle = .overCurrentContext
self.present(addIncidentViewController, animated: true) {
}
And set background color with some alpha.
Solution 3
You can use UIView
instead of a view controller.
Then adjust the alpha of view's background color.
Solution 4
Swift3
Under viewWillAppear
you can use:
self.view.alpha = 0.8
And under viewWillDisappear
you can use:
self.view.alpha = 1.0
Solution 5
AppsDev,
Is this what you are looking for buddy ??? Here Testing1234 is a label added to parentViewController Me here is a label added to childViewController which is presented modally :)
If your answer is yes, Here is how you can do it :) Assuming you are uisng storyboard I'll write the answer :) If you are using Xib dont worry all these properties can be set programmatically as well :)
The main point here is to do the modal presentation
overfull screen
:)
Here is how you can do it :)
Drag a segue between your two ViewControllers :) Make the segue to present the viewController modally and select the following configurations :)
Now select your parent ViewController which will present the secondViewController and change its background color to white (or whatever color you want)
Now select your secondViewController which needs to be presented :) Select its View and set its alpha to 0.5 and color to clear color as shown below
Now add anotherView to the viewController set its color to black and alpha to 0.5 or 0.6 depending on your need for shade :)
Now add whatever the view components you want to add on top of it run and you will se the out put as shown above :)
Hope it helps :)
AppsDev
Updated on July 05, 2022Comments
-
AppsDev almost 2 years
I have a view controller which view's background I need to be translucent and keep showing the view below. I've adjusted the opacity in the
nib
file and tried both pushing the view controller into a navigation stack and presenting it modally, but when loaded, the previous view is unloaded. How could I solve this? -
AppsDev almost 8 yearsYes, I want something like that, but I'm managing separated
nib
files -
Sandeep Bhandari almost 8 years@appsdev : Should not be an issue isnt it buddy :) Simply set its backlground colors the way mentioned and finally when you load it programmatically make sure you present it nodally and over full screen :)
-
AppsDev almost 8 yearsI've set
UIModalPresentationStyle.OverFullScreen
but it doesn't work for me :S -
Sandeep Bhandari almost 8 yearshmmm :) Whats the issue ???? Have you set the background color of your xib to clear color and added a new view on top of it with black color and alpha as 0.5 ???? The output Ive attached is a executing code's output should work fine :)
-
regina_fallangi over 6 yearsIs it possible that this answer just makes the background turn black?. OP mentioned the view should still be seen, but when I change the
alpha
of the view, it is just black. -
Geniouse over 6 yearsThe background should turn black only if the alpha value was 0. In this case it's set at 0.8 so the background would be lightly dimmed.
-
PiyushRathi over 6 yearsAwesome.. Worked for me. .Thanks @Anh Pham
-
Display Name over 6 yearsThis is the proper answer. When I try to pop over a new VC, the original one disappears as soon as the animation finishes. So I rather use a view only for that.
-
hellodear about 5 yearsThis is the best answer on stack over flow for this problem... I have tried many many solutions and finally came to this.. Thank u boss!!
-
Satyam almost 4 yearsHow can I change the opacity?
-
Satyam almost 4 yearsHow to change the opacity?