Presenting a view controller with dim background

28,622

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 :)

enter image description here

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 :) enter image description here

Now select your parent ViewController which will present the secondViewController and change its background color to white (or whatever color you want)

enter image description here

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

enter image description here

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 :)

enter image description here

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 :)

Share:
28,622
AppsDev
Author by

AppsDev

Updated on July 05, 2022

Comments

  • AppsDev
    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
    AppsDev almost 8 years
    Yes, I want something like that, but I'm managing separated nib files
  • Sandeep Bhandari
    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
    AppsDev almost 8 years
    I've set UIModalPresentationStyle.OverFullScreen but it doesn't work for me :S
  • Sandeep Bhandari
    Sandeep Bhandari almost 8 years
    hmmm :) 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
    regina_fallangi over 6 years
    Is 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
    Geniouse over 6 years
    The 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
    PiyushRathi over 6 years
    Awesome.. Worked for me. .Thanks @Anh Pham
  • Display Name
    Display Name over 6 years
    This 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
    hellodear about 5 years
    This 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
    Satyam almost 4 years
    How can I change the opacity?
  • Satyam
    Satyam almost 4 years
    How to change the opacity?