Reputation: 12499
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?
Upvotes: 12
Views: 32411
Reputation: 11140
Imagine situation, that you want to use your view controller in multiple places (let's say you're creating framework). You need to set modalPresentationStyle
to .overCurrentContext
, but you don't want to set it every time you create new instance of view controller.
You want to set it inside overridden init
of view controller which will be presented
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
modalPresentationStyle = .overCurrentContext
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
then in viewDidLoad()
of this controller just background color to lighter shade of black:
override public func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
}
Finally, when you need to present your view controller, just initialize it and present it
let viewController = SecondViewController(nibName: "SecondViewController", bundle: nil)
present(viewController, animated: true)
Upvotes: 1
Reputation: 254
There are two ways to present ViewController with opacity view.
Over current viewcontroller
// presenting view controller
definesPresentationContext = true
// presented view controller
modalPresentationStyle = .overCurrentContext
If view controller has navigation or tabbar over fullscreen.
// presented view controller
modalPresentationStyle = .overFullScreen
modalTransitionStyle = .crossDissolve
Upvotes: 1
Reputation: 2118
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)
Upvotes: 23
Reputation: 189
Swift3
Under viewWillAppear
you can use:
self.view.alpha = 0.8
And under viewWillDisappear
you can use:
self.view.alpha = 1.0
Upvotes: 2
Reputation: 619
(Swift 3) Better solution is to load the view from xib and then add it to the controller like so :
let supportView = Bundle.main.loadNibNamed("YourXIB", owner: nil, options: nil)?[0] as! YourView
let viewController = UIViewController()
viewController.view = supportView
viewController.modalPresentationStyle = .overCurrentContext
present(viewController, animated: true, completion: nil)e
And then add some transparency :
viewController.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
Upvotes: 0
Reputation: 4112
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.
Upvotes: 3
Reputation: 20369
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 :)
Upvotes: 2
Reputation: 10317
You can use UIView
instead of a view controller.
Then adjust the alpha of view's background color.
Upvotes: 3