remove black background when switching tab of `UITabBarController` - ios

I have UITabBarController having 4 tabs. inside the first tab's ViewController i presented Popup using popUpVC.modalPresentationStyle = .overCurrentContext
so i got this(thats perfect as i want):
but now when i switch to secondTab Dollor_Icon, and then immediately come back to firstTab... i get the blackBG instead of transparent BG.
like this:
my code of modal presentation:
let popUpVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopUp")
popUpVC.modalPresentationStyle = .overCurrentContext
self.present(popUpVC, animated: true)
Hope to get rid of that black background and why is this happening?
Thanks!

Add this to your firstTabViewController ViewDidLoad() method:
definesPresentationContext = true
Hope, your problem being fixed.
For more details on definesPresentationContext see https://developer.apple.com/documentation/uikit/uiviewcontroller/1621456-definespresentationcontext'

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.

Make a swipe from left edge to dismiss UIViewController

Example I have two UIViewController (I'll called A and B)
In A, when I click a button, I will show B with code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier:"BController") as! BController
present(viewController, animated: true)
I don't use the navigate ViewController in Storyboard file.
So, does it have any solution to when Applicaion in B, I swipe the screen from left edge and B will dismiss.
You can imagine what I want in this image
Add A Controller to a NavigationController
I make this by code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier:"AController")
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.isNavigationBarHidden = true
self.present(navigationController, animated: true)
then from A
self.navigationController?.pushViewController(bController, animated: true)
You would need to write an interactive custom transition animation. Doing so is complex but well documented in tutorials and examples.

Showing a specific view controller on load

In some cases I want my app to present a different view controller than the one that normally loads (in this case AltView rather than MainView), but I can't figure out how to do it. I tried like this:
let storyboard = UIStoryboard.init(name: "Alt", bundle: nil)
let nav = storyboard.instantiateViewControllerWithIdentifier("AltView")
let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let rootVC = mainStoryboard.instantiateViewControllerWithIdentifier("MainView")
self.window!.rootViewController = rootVC
window!.makeKeyAndVisible()
self.window!.rootViewController?.presentViewController(nav, animated: true, completion: nil)
But nothing happens, the screen is just blank, no errors or warnings are being reported.
EDIT: AltView is an UINavigationController with an embedded UIViewController. The viewDidLoad() method of AltView's embedded view controller is being run, but its viewWillAppear() method is not.
What am I doing wrong?
Just set the window controller like this
let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let nav = storyboard.instantiateViewControllerWithIdentifier("AltView")
self.window!.rootViewController = nav
If u want to change the controller change the window rootviewcontroller again with same code just change the identifier to the one that you want of the controller and you don't need this statement `self.window!.rootViewController?.presentViewController(nav, animated: true, completion: nil)
If you face any issues let me know

Swift - Present another view controller with its navigation bar

I have two ViewControllers -- one with storyboard and one without. Both of those view controllers have their own Navigation Bar at the top. Now when I use self.presentViewController(editorViewController, animated: true, completion: nil) my editorViewController comes up but without its Navigation bar.
Any ideas how to fix this?
I fixed the problem using the following code:
let editorViewController = IMGLYMainEditorViewController()
let navEditorViewController: UINavigationController = UINavigationController(rootViewController: editorViewController)
self.presentViewController(navEditorViewController, animated: true, completion: nil)
I just added the navEditorViewController as it made my navigation bar with its items to appear.
Try self.navigationController!.pushViewController(...)
Swift 5+
let destinationNavigationController = self.storyboard!.instantiateViewController(withIdentifier: "nav") as! UINavigationController
destinationNavigationController.modalPresentationStyle = .fullScreen
self.present(destinationNavigationController, animated: true, completion: nil)
Here your navigation bar replaces with new navigation bar.
So for everyone still curious about this problem, given that we already have existing UINavigationController other than the current one:
Swift 3
First, we need to find the UIViewController that we want to present:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "DestinationViewController") as! DestinationViewController
Next, we're doing the same thing for UINavigationController:
let destinationNavigationController = storyboard.instantiateViewController(withIdentifier: "DestinationNavigationController") as! UINavigationController
Then, we want to bring the DestinationViewController to the top of our destination UINavigationController stack:
destinationNavigationController.pushViewController(destinationViewController, animated: true)
Finally, just present destination UINavigationController:
self.present(destinationNavigationController, animated: true, completion: nil)

iOS Swift - Presenting View Controller sliding down while presenting new View Controller

I present a View Controller in the following fashion:
let vc: ChangeDateViewController = storyboard!.instantiateViewControllerWithIdentifier("changedate") as! ChangeDateViewController
let navigationController = UINavigationController(rootViewController: vc) //ensures that the top navigation bar remains in the new View Controller
self.presentViewController(navigationController, animated: true, completion: nil)
For some reason, the base presenting View Controller slides down while the new View Controller is sliding up. Although the presenting works, it does look glitchy because the sliding down reveals the black background behind the views. Is this a common occurrence, and is there anything I can do to prevent it?
Try this:
let storyboard1 = UIStoryboard(name: "Main", bundle: nil)
let conn = storyboard1.instantiateViewControllerWithIdentifier("changedate") as! LMAddaccountMainVC
self.presentViewController(conn, animated: true, completion: nil)
This might be helpful to solve you issue.

Resources