show full view controller from popover in iOS Swift - swift

I am trying to show a full screen view from a button in a popover view. I tried various codes including the following. Any help would be appreciated. Thanks :)
self.view.presentviewcontroller
self.parentview.presentviewcontroller.

try
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let view2 = storyboard.instantiateViewControllerWithIdentifier("login") as logInViewController
self.presentViewController(view2, animated:true, completion: nil)

Related

Why pushing to a ViewController displays it as popup?

I have been trying to figure this for a while now. When I'm pushing to a ViewController it displays it as a popup instead of the fullscreen.
I have UINavigationViewController embedded in MainPageViewController. Inside MainPageViewController, I have a "Sign in" button that once clicked it should display the HomePageViewController. The question is why it is displayed as a popup instead of fullscreen?
Below is the code that I'm using to push to the HomePageViewController, inside the Sign In button action method:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let destinationNavigationController = storyboard.instantiateViewController(withIdentifier: "NavigationViewController") as! UINavigationController
You have to give ViewController Presentation as a Full Screen
If you need to do it by code, you can set presentation and transition style like this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
destinationViewController.modalPresentationStyle = .fullScreen
destinationViewController.modalTransitionStyle = .coverVertical
And then push from your navigation controller

How to create or present a view controller like this (Facebook Modally way?)

I need to present a view like this, without full-screen, like a popover or popup. I don't know if this presentation style has a name. Does someone know a way to do this? Or someone can tell me where I can found this? Thanks to all.
Open translucent modal controller :
class ViewController: UIViewController {
func openTranslucentController() -> ModalController {
let storyboard: UIStoryboard = UIStoryboard(name: "Controller", bundle: nil)
let controller: ModalController = storyboard.instantiateViewController(withIdentifier: "ControllerIdentifier") as! ModalController
controller.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
controller.modalPresentationStyle = .overCurrentContext
self.present(controller, animated: true, completion: nil)
}
}
Now, you can design it in the required way.

How to programmatically show modal in a UINavigationController

In my app, I have a UINavigationController that i'm pushing and popping ViewControllers from. At some point, I want to show a VC modally (showing the previous controller "underneath"). I can get it to work by setting up a segue in a storyboard, however I'm in a spot where I need to do it programmatically, and I can't seem to find the right magic incantation to make it work.
I saw a couple of similar questions but they seemed to be showing the UINavigationController modally, not showing one of the VC's on the UINavigationController stack modally.
(I put up a test application here: https://github.com/SuperTango/ModalNavController, and that's where this code and images come from)
The "Manual" code does:
#IBAction func goToVC2Tapped(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "VC2ViewController") as! VC2ViewController
destinationViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.navigationController?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.navigationController?.pushViewController(destinationViewController, animated: true)
}
but it's not working (see the second transition in the gif below).
The segue that works is setup like this:
This gif is from the test app and shows how it works with the segue, but not manually.
Any ideas? Thanks!
To present modally you need to use:
present(destinationViewController, animated: true, completion: { })
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "YourVC") as! YourVC
destinationViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
destinationViewController.modalTransitionStyle = .crossDissolve
self.present(destinationViewController, animated: false, completion: nil)
I think this helps show your ViewController in a modal way of presenting. Write the above code under outlet actions or where you want to present your ViewController.

show a ViewController from a Detail View Controller in a UISplitViewController

My aim is: To show a popUpViewController in my project in which the user can add a "lesson" (adding a tableViewCell to a tableView) and then save it.
So when the user presses a button inside the DetailViewController of a UISplitViewController , the popUpViewController should show.
I also want the pop-up to be shown as fullscreen with just a small window in the middle and the rest should be black with 0.5 alpha, transparent.
My problem is: I can't find out how I have to show the popUp.
What I've tried and my results:
Tried: Showing it with present(Viewcontroller, animated:, completion: )
Result: It shows fullscreen but the other VC isnt visible in the background anymore.
Tried: The following snippet:
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popUpVC") as! PopUpViewController
self.addChildViewController(popOverVC)
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
Result: The view isnt fullscreen but only the size of the detail View Controller (doesnt cover the part of the masterViewController on the left.
Thanks for your help.
You might try again with present(Viewcontroller, animated:, completion: )
and using modalPresentationStyle with formSheet eg:
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popUpVC") as! PopUpViewController
popOverVC.modalPresentationStyle = .formSheet
self.present(popOverVC, animated: true, completion: nil)

How to reset/restart viewcontroller in Swift?

I want to have a button on my navigation bar "Reset" and I would like this to be connected to an IBAction to sort of "restart" the controller.
I have some segues from another controller that changes some aspects of the viewcontroller (that has a collectionview) and I want the user to be able to start over. Does anyone have any suggestions as to how to proceed?
If you embed navigation controller to your view controller then you can use this code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController")
let viewcontrollers = self.navigationController.viewControllers
viewcontrollers.removeLast()
viewcontrollers.append(vc)
self.navigationControllers?.setViewControllers(viewcontrollers, animate: true)
You can change rootViewController of your application:
UIApplication.shared.keyWindow?.rootViewController = UIViewController()
This is how I do it in Swift 2.1 with my Home view inside a UINavigationController:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let homeVC = storyboard.instantiateViewControllerWithIdentifier("HomeViewController")
self.navigationController?.presentViewController(homeVC, animated: false, completion: nil)
self.navigationController?.dismissViewControllerAnimated(false, completion: nil)