Presenting a view in fullscreen not working - ios

I am trying to present a view that is showd from the bottom to the top. This is my code
let myPageViewController = storyboard?.instantiateViewController(withIdentifier: "myPageViewControllerID") as! MyPageViewController
myPageViewController.modalPresentationStyle = .fullScreen
let navController = UINavigationController(rootViewController: myPageViewController)
navigationController?.present(navController, animated: true, completion: nil)
Despite I am using .fullScreen, the view is not presented on full screen.
I tried using peformSegue to show the view with this code
self.performSegue(withIdentifier: "myPageViewSegue", sender: nil)
the page is presented on fullscreen but from the left to the right and not from the bottom to the top.
The third code I tried is this one
let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .fullScreen
present(detailVC, animated: true)
here I get an error Application tried to present modally an active controller. I tried to add self.dismiss when disappearing MyPageViewController but it didn't helped.

The problem maybe is you are not presenting navigationController you are presenting detail vc, Use this
'let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .fullScreen
present(navigationController, animated: true)'
if problem still exists use
navigationController.modalPresentationStyle = .overCurrentContext

Try this one:
let detailVC = MyPageViewController()
detailVC.modalPresentationStyle = .fullScreen
present(detailVC, animated: true)

You could try this, worked for me:
Setup up your segue:
performSegue(withIdentifier: "myPageViewSegue", sender: self)
Then use the prepare for segue method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destVC = segue.destination as! MyPageViewSegue
destVC.modalPresentationStyle = .fullScreen
}

I ran into this problem and was stumped. Then I realized that overriding dismiss also overrides .fullscreen
// This breaks .fullscreen
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: flag, completion: completion)
// do something
}
It's clear that you don't need this if .fullscreen is being used, but leaving it in place caused problems.

You need to change your code with below lines and then Booom work like a charm
let vc = MyPageViewController()
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true)

let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .overCurrentContext
present(detailVC, animated: true, completion: nil)
Explanation:
Its very simple. Its better to explain it line by line. Line 1: Declare the viewcontroller. Line 2: Add Navigation Controller (its optional. If you don't need, then you may skip navigation bar). Line 3: We have defined the modal presentation style link as overCurrentContext. and present the navigationController (it will come up with navigation bar and viewcontroller) or only the viewcontroller as per your need. In this piece of code, you will get the viewcontroller without navigation bar.

Related

When calling a function to switch to another ViewController, the application crashes

Error text:
Application tried to present modally an active controller.
Moreover, all elements seem to be called for the first time. The application crashes most likely due to the navigation controller. How can I fix this error?
#objc private func taskButtonTouched(){
let vc = TaskVC()
let navigationController = OptionalNC(rootViewController: vc)
navigationController.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)
}
You must present OptionalNC insetead TaskVC
#objc private func taskButtonTouched(){
let vc = TaskVC()
let navigationController = OptionalNC(rootViewController: vc)
navigationController.modalPresentationStyle = .fullScreen
self.present(navigationController, animated: true, completion: nil)
}

Navigate to ViewController and dissmiss VC

Here i'm dismissing one VC and trying to navigate new VC , but navigation not working. Dismiss VC working fine.
#IBAction func onClickEmailBtn(_ sender: UIButton) {
//dismiss VC
self.dismiss(animated: false, completion: nil)
//Navigate to VC
DispatchQueue.main.async {
let cevc = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController
self.navigationController?.pushViewController(cevc, animated: true)
}
}
Here I have one NavigationViewController called VC1, on that I'm presenting one VC called VC2. In this VC2 when I click button I want to navigate new VC called EmailVC.
try this code
#IBAction func onClickEmailBtn(_ sender: UIButton) {
if let controller = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController {
self.dismiss(animated: false, completion: nil)
self.presentingViewController?.present(controller, animated: true, completion: nil)
}
}
You were trying to push a ViewVontroller from dismissing view controller,
and the answer which you have accepted is presenting a viewController from dismissing viewController. Although it may serve your purpose, but I am going to answer you original question
// get the object of presenting nanvigation controller(navigation controller with has presented this view controller)
let presentingNavigationController = presentingViewController?.navigationController
dismiss(animated: true) {
let cevc = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController
// here you push your view controller with presentingNavigationController
presentingNavigationController?.pushViewController(cevc, animated: true)
}
Try to find out Navigation controller using below code and replace Viewcontroller with your Controller name.
let productVC = SeconViewViewController()
let parentVC = (parent!.presentingViewController as! ViewController).childViewControllers.first as! UINavigationController
self.navigationController?.dismiss(animated: true, completion: {
parentVC.pushViewController(productVC, animated: true)
})

Show BlurEffect on manual segue

I have a question regarding blureffects on a view controller. I program all of my segues manually for several reasons. But when I go to a viewcontroller that has a blureffect background, I only see a dark gray background. How can I solve this that the view controller blur effect lays on top of another viewcontroller where the content of the previous viewcontroller can be seen through?
The blureffects are now applied by storyboard with transparant background views.
My code for seque is:
Action:
#IBAction func ToUserSections(_ sender: Any) {
let controller =
Navigation.getDestinationViewControllerWith("User",
controllerName: "UserSectionsVC")
present(controller, animated: false, completion: nil)
}
Class:
class Navigation{
static func getDestinationViewControllerWith(_
storyboardName:String, controllerName:String)-> UIViewController{
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let controller =
storyboard.instantiateViewController(withIdentifier:
controllerName) as UIViewController
return controller
}
}
Hope you can help me with this because it will maximize aesthetics for the app :D
try this.
let viewController : CustomViewControlller = UIStoryboard.getMainStoryboard().instantiateViewController(withIdentifier: "Identifier") as! CustomViewControlller
viewController.modalPresentationStyle = .overCurrentContext
viewController.modalTransitionStyle = .crossDissolve
self.present(viewController, animated: true, completion: { _ in })
Alright for others to know, what worked for me was the following:
At IBAction: adding the following line before presenting
controller.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
Like this:
#IBAction func ToUserSections(_ sender: Any) {
let controller = Navigation.getDestinationViewControllerWith("User", controllerName: "UserSectionsVC")
controller.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
present(controller, animated: false, completion: nil)
}

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)

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