Swift 4 present view controller black screen - ios

I'm presenting another view controller like this:
func goToScreen(id : String) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: id)
self.present(newViewController, animated: true, completion: nil)
}
The problem is, there is a cc. 1 second delay between the appearance of the new viewcontroller, and in the meantime the app shows an all black screen. Why is that? It looks really ugly

change status of animation to false while move to next view controller this will remove the delay. Delay happening only because of animation: true.
Use below code to solve your issue
self.present(newViewController, animated: false, completion: nil)
Hope this will help you

I was inheriting from UITabBarController instead of UIViewController from the screen I was pushing to.

Related

Swift - popViewController after Dismiss

I'm trying to dismiss viewcontroller presented as modal("Present Modally") and then popViewController in navigationController. Here is my storyboard, I want to go from QRScanner VC to Monitoring VC.
Here is how I'm presenting QRScanner VC in Add Device VC:
let storyboard = UIStoryboard(name: StoryboardIDs.MainStoryboard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: VCIDs.QRDeviceScannerVC) as! QRDeviceScannerVC
controller.deviceName = deviceNameTxt.text
present(controller, animated: false, completion: nil)
Here is how I'm trying to go back to MonitoringVC:
self?.navigationController?.popViewController(animated:true)?.navigationController?.popViewController(animated: false)
Also tried:
self?.dismiss(animated: true, completion: {
self?.presentingVC?.navigationController?.popViewController(animated: false)
})
It always goes to the Add Device VC instead of Monitoring VC
Use an unwind segue, with the unwind target method located in the Monitoring VC. All the right stuff will just happen as if by magic.
You can try with
self?.dismiss(animated: false, completion:nil)
self?.presentingVC?.navigationController?.popViewController(animated: true)
Just check if you have reference to the navigation controller

Swift 4 push is not working in Storyboards

I am trying to push a VC to different VC. Till this date, I have worked a lot with .xib files. So, now moving to storyboard I am facing a little problem. I have two storyboards A.storyboard and B.storyboard
What I have tried is this:
let storyBoard: UIStoryboard = UIStoryboard(name: storyboard, bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: storyboardId)
controller.present(newViewController, animated: true, completion: nil)
This works fine for me, but I am worried about memory leakage. The other way which I know and prefer is this:
self.navigationController?.pushViewController(LoginController(), animated: true)
But, when I use this ^ I get a black screen and nothing. Why the push is not working here, but works fine for me in .xibs?
Your first code is correct, but it is not a push — it is a present. So you should change your code, like this:
let storyBoard: UIStoryboard = UIStoryboard(name: storyboard, bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: storyboardId)
self.navigationController?.pushViewController(newViewController, animated: true, completion: nil)
The problem with the second code is that LoginController() is not the login controller in the storyboard — it's just a new blank LoginController.

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.

How to switch view controllers in swift?

I'm trying to switch from one UIViewController to another using code. Right now I have,
self.presentViewController(ResultViewController(), animated: true, completion: nil)
All this is doing is taking me to a black screen instead of the ResultViewController. Any suggestions?
With Storyboard. Create a swift file (SecondViewController.swift) for the second view controller
and in the appropriate function type this:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
Without storyboard
let secondViewController = ViewController(nibNameOrNil: NibName, bundleOrNil: nil)
self.presentViewController(secondViewController, animated: true, completion: nil)
You can try this one
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("ResultView") as ResultViewController
self.presentViewController(resultViewController, animated:true, completion:nil)
Make sure you set your view controller identifier.
Swift 3
To present a controller modally
let vc = self.storyboard!.instantiateWithIdentifier("SecondViewController")
self.present(vc, animate: true, completion: nil)
To show a controller
self.show(vc, sender: self)
Try this:
let vc = ViewController(nibNameOrNil: yourNibName, bundleOrNil: nil)
self.presentViewController(vc, animated: true, completion: nil)
Hope this helps.. :)
The easiest way to do this would be to create a segue by right clicking on the view you are starting on and dragging the blue line to the result view controller the hit the show segue option. Then set the identifier of the segue to "segue". Now add this code wherever you would like in your view controller:
self.performSegueWithIdentifier("segue", sender: nil)
this will trigger the segue that will go to your resultViewController
The easiest way to switch between screens in iOS is. Add a button on the current screen. Then press control and drag the button to the target screen. Then select push. And when you will tap the button the screen will be switched to the target screen.
Make sure you are using a UINavigationViewController.
Source: ios UINavigationController Tutorial.
Shaba's answer is a good start but requires the following so that you
can control the segue from within your view controller:
override func shouldPerformSegue(withIdentifier identifier: String,
sender: Any?) -> Bool {
// your code here
// return true to execute the segue
// return false to cause the segue to gracefully fail }
Source
vc = YourViewController()
UIApplication.shared.keyWindow?.rootViewController = vc
Or
vc = YourViewController()
UIApplication.shared.keyWindow?.rootViewController = UINavigationController(rootViewController: vc) //If you want to add navigation functionality to that VC

Resources