Sometimes we have to show a transparent View on a ViewController, For that we generally use .xib file and add them as subView in ViewController to show the transparency. But we can also give the transparency to view controller while presenting this.
![](http://findnerd.s3.amazonaws.com/imagedata/8977/8977.jpg)
So here are the basic steps you need to follow to present a ViewController with transparent background
1- Create a new project.
2- You will see a already created ViewController on the story board.
3- Add another ViewController that you want to present(i.e PresentViewController).
4- set the background color of PresentViewController with some alpha.
5- Create a segue from ViewController to PresentViewController.
6- Set the segue kind to Present Modally in storyBoard also give a identifier to segue something like this - “transparentSegueId” .
7- Add a button on the ViewController To present the Transparent ViewController.
8- Create an action for this button in ViewController class.
@IBAction func presnetMyVC(){
performSegue(withIdentifier: "transparentSegueId", sender: nil)
}
Add another method for handling the segue.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "transparentSegueId"{
let destinationVC = segue.destination
destinationVC.modalPresentationStyle = .overCurrentContext
}
}
Output-> Screen 1 ViewController from we presenting the another viewController.
![](http://findnerd.s3.amazonaws.com/imagedata/8971/8971.png)
Screen 2 - Controller we are presenting with transparency
![](http://findnerd.s3.amazonaws.com/imagedata/8972/8972.png)
0 Comment(s)