Application stuck while presenting new VC in swift - ios

I'm trying to present a new VC, but when I hit the code application get stuck and I can't do anything. It's quite a weird issue, how can I get out of it? My code for presenting new VC is this,
let vc : PropertyDetailController! =
UIStoryboard.viewController(identifier: "PropertyDetailController", storyBoard: "Explore") as? PropertyDetailController
vc.propertyDetailData = property
vc.hidesBottomBarWhenPushed = true
vc.modalTransitionStyle = .crossDissolve
vc.modalPresentationStyle = .fullScreen
self.navigationController?.present(vc, animated: true, completion: nil)
This is what it shows in console,
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
It isn't present new VC. This is how it my apps stuck please check gif link,
enter link description here

As I can see in your code you are moving to the new storyboard so it's necessary to move it on through the UINavigationController
Blow one is the sample code that might help you
let storyBoard: UIStoryboard = UIStoryboard(name: "YOUR_STORYBOARD", bundle: nil)
let aVC: YOUR_VC = storyBoard.instantiateViewController(withIdentifier: "YOUR_VC_ID") as! YOUR_VC
let navigationController = UINavigationController(rootViewController: aVC)
self.present(navigationController, animated: true, completion: nil)

if you want to present a view controller, try
self.present(vc, animated: true, completion: nil)
if you want to push a view controller, try
self.navigationController?.pushViewController(vc, animated: true)

Try moving the push code to main thread,
DispatchQueue.main.async {[weak self] in
self?.navigationController?.pushViewController(vc, animated: true)
}
Either present or push, all the UI handling must be done on the main thread.
Edit:
If you want to present vc, use
DispatchQueue.main.async {[weak self] in
self?.present(vc, animated: true, completion: nil)
}

First make sure current view controller embedded with Navigation Controller.
Now use below code to fix this issue.
let storyboard = UIStoryboard(name: "Explore", bundle: nil)
guard let vc = storyboard.instantiateViewController(withIdentifier: "PropertyDetailController") as? PropertyDetailController else {return}
vc.hidesBottomBarWhenPushed = true
vc.modalTransitionStyle = .crossDissolve
vc.modalPresentationStyle = .fullScreen
self.navigationController?.present(vc, animated: true, completion: nil)

Related

Cannot display View (Programmatically)

I'm learning how to make an app with just a code in Swift. I have encountered this problem:
This is action of a button.
#objc func answerAction() {
let story = UIStoryboard(name: "Main", bundle: nil)
let controller = story.instantiateViewController(withIdentifier: "AccountViewController") as! AccountViewController
self.present(controller, animated: true, completion: nil)
}
If I press it, it shows this error:
Thread 1: "Could not find a storyboard named 'Main' in bundle NSBundle </Users/mas/Library/Developer/CoreSimulator/Devices/A3BEC6D0-3AA6-4193-A755-1181DD580576/data/Containers/Bundle/Application/C80D5D36-EBD8-44D4-AF14-B64E2E7E5587/AppForTest.app> (loaded)"
As I understand, the problem is that I have deleted Main.storyboard and app cannot reach it. So how I should declare in a answerAction story constant?
#objc func answerAction() {
let controller = AccountViewController()
self.present(controller, animated: true, completion: nil)
}

How do I force a ViewController to present without the user pressing any buttons?

When a function is called, I would like SixthViewController to be presented to the user without the user needing to press any buttons. I know how to present another ViewControllerby setting a button to "Show" another ViewController, but how can I programmatically force SixthViewController to present itself once the function is called?
you can present viewcontroller with this way:
let destVC = SixthViewController()
self.present(destVC, animated: true, completion: nil)
or
let storyboard: UIStoryboard = UIStoryboard(name: "storyboardNamehere", bundle: nil)
let destVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerIdentifierhere" as! SixthViewController
self.present(destVC, animated: true, completion: nil)
========================================================================
just write down this function and also called it when u want to push or write down it in any function like func pushViewController and called it self.pushViewController() that's it bro.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let sixthViewController = storyBoard.instantiateViewController(withIdentifier: "SixthViewController") as! SixthViewController
self.navigationController?.pushViewController(sixthViewController, animated: true)
If you are not using segues but navigation controller try this:
let destinationVC = SixthViewController()
self.navigationController?.pushViewController(destinationVC, animated: true)
If you just want to show it without navigation controller:
let destinationVC = SixthViewController()
self.present(destinationVC, animated: true, completion: nil)
If you are using storyboard:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let destVC = self.storyboard?.instantiateViewController(withIdentifier: "SixthViewController" as! SixthViewController
self.present(destVC, animated: true, completion: nil)

How can I dismiss an overCurrentContext modal viewController and its children?

I have the following structure:
From mainVC I present a modal viewController, modalVC:
let modalVC: ModalVC = ModalVC()
modalVC.modalPresentationStyle = .overCurrentContext
modalVC.modalTransitionStyle = .coverVertical
self.present(modalVC, animated: false, completion: nil)
From modalVC I present another viewController chatVC, but this time without the .overCurrentContext option:
let chatVC: ChatVC = ChatVC()
self.present(chatVC, animated: false, completion: nil)
At this point, I have the following:
mainVC --present modally --> modalVC --present--> chatVC
I want to dismiss chatVC and modalVC to show mainVC again.
I tried:
self.presentingViewController?.presentingViewController?.dismiss(animated: false, completion: nil)
No succeed.
Any ideas? Thanks in advance
It can sometimes help to be a little verbose... helps to understand what's going on vs. what you expect to be going on. Try it like this:
if let myPresenter = self.presentingViewController {
if let hisPresenter = myPresenter.presentingViewController {
hisPresenter.dismiss(animated: false, completion: nil)
}
}
If that doesn't "work" you can at least step through in debug to figure out what's not quite right.

How can I switch view controllers with code in iOS?

let SecondViewController =
self.storyboard?.instantiateViewController(withIdentifier:
"SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(SecondViewController,
animated: true)
For some reason this doesn't work. Any ideas?
Replace
self.navigationController?.pushViewController(SecondViewController,
animated: true)
with
self.navigationController?.present(SecondViewController, animated: true, completion: nil)
Edit: To avoid the optionals you should also do one of the following:
guard let navController = self.navigationController else { return }
navController.present(SecondViewController, animated: true, completion: nil)
or
if let nacVontroller = self.navigationController {
navController.present(SecondViewController, animated: true, completion: nil)
}
Edit2: You should also avoid force unwrapping your SecondViewController using one of the above methods as well. Although that is not your current issue.

UIModalTransitionStyle.FlipHorizontal doesn't flip

I am new in Cocoa and Swift, and I'm trying to use the UIModalTransitionStyle.FlipHorizontal transition to switch from ViewController1 to ViewContoller2 and vice-verse.
Unfortunately the switch from controller 1 to controller 2 occurs without any transition, but works correctly on dismiss.
I use this code in ViewController1:
let ctrl = ViewController2(nibName: nil, bundle: nil)
ctrl.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
self.presentViewController(ctrl, animated: true, completion: nil)
and this code in ViewControler2:
self.presentingViewController.dismissViewControllerAnimated(true, completion: nil)

Resources