Swift ViewController Background getting black with alpha - ios

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)

Related

How to fix width of messageInputBar in MessageKit?

I'm presenting the ChatViewController inside a UINavigationViewController as a modal pagesheet, however, as you can see from the image below, the messageInputBar is stuck at the bottom and is occupying the entire width of the screen. Is there a way for me to set the messageInputBar attached to the chat area and with the same width as the chat area?
here is the code i have currently for presenting the ChatViewController
let newVC = UIViewController()
newVC.add
let navVC = UINavigationController(rootViewController: chatVC)]
navVC.isModalInPresentation = true
navVC.viewControllers = [chatVC]
navVC.modalPresentationStyle = .pageSheet
self.present(navVC, animated: true)

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)

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.

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

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