How to present ViewController as popup VC with transparent background? - ios

I want to present ViewController as a popup ViewController, it's working good but the problem is background color getting black, but I want a transparent background color.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ADLVC") as! ListViewController
//remove black screen in background
vc.modalPresentationStyle = .overCurrentContext
//add clear color background
vc.view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
//present modal
self.navigationController?.pushViewController(vc, animated: false)
With this code, I can present a transparent popup ViewCcontroller but when click on the close button in popup ViewController viewWillAppear() not calling.
//create view controller
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SVC") as! SViewController
//remove black screen in background
vc.modalPresentationStyle = .overCurrentContext
//add clear color background
vc.view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
//present modal
self.present(vc, animated: false, completion: nil)

in vc, use [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4] to set the backround color and delete vc.modalPresentationStyle = .overCurrentContext, hope it work!

It is not possible to make UIView transparent, until it is subview of some other UIVIew.
To achieve what you are trying, you can add that view controller as child view controller of presenter. and add child view controller's view as subview to parent view controller's view.
Have a look:
let childViewController be the one you want to present.
then do this in presenter(parent) view controller.
presenter.addChild(childViewController)
childViewController.view.backgroundColor = UIColor.clear
presenter.view.addSubView(childViewController.view)
childViewController.didMove(toParent: presenter)
You can add animations as well.
Hope this will help.

If you would like to present your ViewController as popup. you can use the UIWindow + modalPresentationStyle + modalTransitionStyle. like that:
let storyboard = UIStoryboard(name: "ADLVC", bundle: nil)
let listViewController = storyboard.instantiateViewController(withIdentifier: "ListViewController")
listViewController.modalPresentationStyle = .overFullScreen
listViewController.modalTransitionStyle = .crossDissolve
self.window?.rootViewController!.present(metarTAFVC, animated: true, completion: nil)

Related

How to present a popup view in full screen

I've following view configuration
Tab bar -> Nav controller -> View: Click on a button -> segue to-> Another view: Click on a button -> Popup view.
This modal view is on a different storyboard.
I want to present this model view in full screen. I've tried this solution mentioned on Presenting modal in iOS 13 fullscreen but it doesn't work. I've also tried few other solutions but the popup view is not showing over full screen, status bar is visible at the top.
How do I present modal view in a full screen?
let storyboard = UIStoryboard(name: "Other", bundle: Bundle.main)
guard let popupVC = storyboard.instantiateViewController(withIdentifier: "PopUpViewController") as? PopUpViewController else {
print("PopUpViewController not found")
return
}
popupVC.modalPresentationStyle = .fullScreen
self.present(popupVC, animated: true, completion: nil)
You could try presenting with the navigation controller.
let storyboard = UIStoryboard(name: "Other", bundle: Bundle.main)
guard let popupVC = storyboard.instantiateViewController(withIdentifier: "PopUpViewController") as? PopUpViewController else {
print("PopUpViewController not found")
return
}
var navigationController = UINavigationController(rootViewController: popupVC)
navigationController.modalPresentationStyle = .fullScreen
self.present(navigationViewController, animated: true, completion: nil)
An alternative would be to use presentViewController but presentViewController will only present one viewController modally over the currently visible viewController whereas presenting with the navigationController will give the flexibility to push further components on top, providing a smoother navigation experience with go back to previous page kind of behaviour.
Maybe try creating the view controller this way:
let popOverVC = UIStoryboard(name: "yourBoard", bundle: nil).instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
self.addChild(popOverVC)
let lSs = UIScreen.main.bounds
popOverVC.view.frame = CGRect(x: 0, y: 0, width: lSs.width, height: lSs.height)
popOverVC.view.tag = tag
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParent: self)
Within the popup view controller class, add a method for animating its view in viewDidLoad.
Edit: I read your replies. The issue you are having is caused by not handling the status bar properly. Instead of trying to make this veiewcontroller fullscreen, simply hide the status bar
How do I hide the status bar in a Swift iOS app?
and bring it back when you want to see it again.

Change Background Color of modal view

I would like to resent an viewcontroller as modal view in size .formSheet. The background color should not be grey transparent, it should be an blur effect.
How I cloud change the background color of view behind modal.
let storyboard = UIStoryboard(name: "DetailViewController", bundle: nil)
if let modalViewController = storyboard.instantiateInitialViewController() as? DetailViewController {
self.definesPresentationContext = true
self.providesPresentationContextTransitionStyle = true
modalViewController.item = item
modalViewController.modalPresentationStyle = .formSheet
modalViewController.modalPresentationCapturesStatusBarAppearance = true
self.present(modalViewController, animated: true, completion: nil)
}
From the viewWillAppear(:) method of your modalViewController, you can access the self.presentationController?.containerView property. You can either change the background color or even add a blur effect view if you want.

Swift ViewController Background getting black with alpha

I'm calling a ViewController as popover when the user presses a button. The View should have black background with alpha 0.5.
But the View is shown as that for a second, than the whole background turns black without alpha. Any idea why?
Thats my popover call:
let popOver = storyboard?.instantiateViewController(withIdentifier: "popOver") as! ViewControllerPopOver
popOver.modalPresentationStyle = .popover
self.present(popOver, animated: true, completion: nil)
I'm trying to set the background color in popovers viewDidLoad() function with following code:
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
For that set modalPresentationStyle to overCurrentContext instead of popover.
let popOver = storyboard?.instantiateViewController(withIdentifier: "popOver") as! ViewControllerPopOver
popOver.modalPresentationStyle = .overCurrentContext
self.present(popOver, animated: true)

Background not transparent when modal is in NavigationController

I'm having a viewcontroller that has a blurview as background so that you can see the underlying viewcontroller.
This works great except when you want to present the modalviewcontroller in a navigationController. Then you see the blurview for a sec and then it is just white and you can't see the underlying viewcontroller.
I tried to set the navigationcontroller.view.backgroundColor to clear but this doesn't work.
How can I achieve this?
let vc: FilterViewController = FilterViewController()
vc.modalPresentationStyle = .overFullScreen
vc.delegate = self
let navCtrl = UINavigationController(rootViewController: vc)
navCtrl.view.backgroundColor = .clear
self.navigationController?.present(navCtrl, animated: true, completion: nil)
you want a screen to blur another screen? it's not so easy but possible...
it's a bit difficult to paste this here, so will give you a link

Replace rootViewController behind a modal view

With the following code I'm able to change the rootViewController behind a modal view :
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let signupVC = storyboard.instantiateViewControllerWithIdentifier("SignupNavigationController") as! UINavigationController
let landingPageVC = storyboard.instantiateViewControllerWithIdentifier("LandingPage") as! LandingPageViewController
presentViewController(signupVC, animated: true, completion: {
UIApplication.sharedApplication().delegate?.window??.rootViewController = landingPageVC
UIApplication.sharedApplication().delegate?.window??.sendSubviewToBack(landingPageVC.view)
})
When I close my modal view controller it's the previous rootViewController that is displayed (only during the animation). Then my new view controller is displayed properly.
Any idea how I could prevent this ?
Answering to myself,
The view I can see during the modal animation is something like a snapshot. I found out that the view is not visible in Xcode's view hierarchy debugger, thus the view is not present in the viewController tree.
To refresh this snapshot I had to set the modalPresentationStyle to .OverFullScreen
myModalViewController.modalPresentationStyle = .OverFullScreen

Resources