Change Background Color of modal view - ios

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.

Related

Navigation bar height decreased when push ios13

When i move to view controller after making navigationbar.ishidden = false the navbar height decreases and there is a black space between navbar and uiview
And when i move down the notification screen navbar height increase
Code
viewController = PlaceOrderViewController(nibName: "PlaceOrderViewController", bundle: nil)
viewController.hidesBottomBarWhenPushed = true
viewController.navigationController?.isNavigationBarHidden = false
viewController.modalPresentationStyle = .fullScreen
viewController.fromLastScreen = "ProductDetailViewController"
self.navigationController?.pushViewController(viewController, animated: true)

How to make a UIView transparent while presenting it from UITableViewCell with UITapGestureRecognizer?

I presented a UINib (custom AlertView) from UITableviewCell by tapping on UILabel. UINib is having two views, one is for custom alert and the background view is to show an alert view effect by making it transparent.
#objc func taptakeYourMedsDescriptionLabel(tapGestureRecognizer: UITapGestureRecognizer) {
let undectableVC = UndetectableAlertVC(nibName: "UndetectableAlertVC", bundle: nil)
undectableVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.1)
UIApplication.shared.keyWindow?.rootViewController?.present(undectableVC, animated: true, completion: nil)
}
I also gave undectableVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.1) in viewWillAppear of UndetectableAlertVC
Problem: The view is transparent while presenting it but soon after the view is presented it is changing to black color.
Requirement:
Can anyone help me with this?
Try This
let undectableVC = UndetectableAlertVC(nibName: "UndetectableAlertVC", bundle: nil)
undectableVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
undectableVC.modalPresentationStyle = .custom
undectableVC.modalTransitionStyle = .crossDissolve
self.navigationController?.present(undectableVC, animated: true, completion: nil)

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)

UINavigationBar set Background, and set translucent not working

I am trying to set the background and trasparecy for UINavigation bar in code, in the ViewWillAppear func. however it doesn't seem to be working.
self.navigationController!.navigationBar.translucent = false
self.navigationController!.navigationBar.backgroundColor = UIColor.blackColor()
the view controller is load via the storyboard id
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier(storyboardId)
vc.title = storyboardId
let navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: false, completion: nil)
i have tried setting it there too, doesn't seem to be working.
additionally i tried with UINavigationBar.appearance() to set the properties, this too doesn't work.
You can try with this:
self.navigationController?.barTintColor = UIColor.blackColor()

Resources