Background not transparent when modal is in NavigationController - ios

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

Related

Swift present viewController issue

I have a ViewController that I present like this:
guard let vc = storyboard?.instantiateViewController(withIdentifier: "calendarViewController") as? FSCalendarViewController else {return}
vc.modalPresentationStyle = .custom
present(vc, animated: true)
Is there any way I can present it modally so I can swipe to close the presented vc without making the previous VC smaller? I feel like most big apps are using that functionality.
you can try to use UIModalPresentationStyle.overCurrentContext

New iOS 13 Modal Presentation: Presenting Controller Doesn't Move Down

I have a weird behavior when presenting UIViewControllers modally in iOS 13. The new presentation style that I've seen all across iOS 13 looks like this:
The presenting view controller appears behind the presented view controller. It is also shifted down to mimic a "stack"
Meanwhile, when presenting view controllers through my app, I keep getting this effect:
The presenting view controller doesn't move at all when presenting a new view controller
I use this code to present this view controller:
let controller = storyboard?.instantiateViewController(withIdentifier: "tutorial") as! TutorialController
controller.modalPresentationStyle = .pageSheet
controller.modalTransitionStyle = .coverVertical
present(controller, animated: true, completion: nil)
Here is my question:
I'm wondering why this is happening and if there is a way to present view controllers in the normal iOS 13 style (with the presenting view controller moving back).
Thanks in advance!
Turns out the problem was my view controller hierarchy. I was able to fix it by making the presenting view controller the root view controller of my app. First I set the background controller as the root view controller by calling
window.rootViewController = self
and then using my previous code
let controller = storyboard?.instantiateViewController(withIdentifier: "tutorial") as! TutorialController
controller.modalPresentationStyle = .pageSheet
controller.modalTransitionStyle = .coverVertical
present(controller, animated: true, completion: nil)
I presented the view controller. Thanks to everyone who tried to help!
I think the issue can be resolved by using vc.modalPresentationStyle = .fullScreen if there is not UINavigationController , otherwise you can use these codes as follows:
let navigationController = UINavigationController(rootViewController: vc)
navigationController.modalPresentationStyle = .fullScreen
present(vc, animated: true)
because With iOS 13 this is a new feature that Apple has changed the default presentation style of View Controllers to a modal sheet from fullscreen in iOS 12
Programmatically:
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)
From storyboard:
That's it. No need to play with root controller or window at all.
For reference, visit this article.
iOS13: Let's say you have 3 view controllers: FirstVC, SecondVC, ThirdVC
FirstVC presents SecondVC with the below code:
let secondVC = SecondVC()
secondVC.modalPresentationStyle = .fullScreen
firstVC.present(secondVC, animated: true, completion: nil)
SecondVC presents ThirdVC with the below code:
let thirdVC = ThirdVC()
secondVC.present(thirdVC, animated: true, completion: nil)
Changing secondVC's modalPresentationStyle to .currentContext solves the problem.
We can change it in the Inspector Tool Bar. To Achieve it: head over to the fifth section of Inspector Tollbar then change the Presentation field to Full Screen.
This should be the only property you need to set
presentedViewController.modalPresentationStyle = .automatic
Detailed in
https://developer.apple.com/videos/play/wwdc2019/224/

How to present ViewController as popup VC with transparent background?

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)

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)

UIVisualEffectView as an overlay

I have a UIViewController that presents another ViewController like that:
let blurryVC = BlurryViewController(names: ["name1, "name2", "name3"])
let navigationC = UINavigationController(rootViewController: blurryVC)
self.modalPresentationStyle = .overFullScreen
self.present(navigationC, animated: true, completion: nil)
on BlurryViewController I have a UIVisualEffectView, when I present this VC the blur works for around 1 second and then it become black. I think something happening the the view of the VC behind.
How can I prevent it from being black when I present it?
Thank you!

Resources