I am facing a problem, because there are two ways how do I display my ViewController
.
performSegue(withIdentifier: "segue", sender: self)
It works fine because then I have this back button in my navigationItem
:
Then I use this code to present the same ViewController
(from another viewController
than in the first case):
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navVC = storyboard.instantiateViewController(withIdentifier: "navViewController") as! UINavigationController
let vc = navVC.topViewController as! ViewController
self.present(navVC, animated: true, completion: nil)
but then I do not have any back button in my ViewController
.
My question is: How can I keep my backButton (exactly how it is)
when I use this function: performSegue(withIdentifier: "segue", sender: self)
, but add button (can look different) when I use this function: self.present(navVC, animated: true, completion: nil)
Note: In my case 1 my segue is connected right to ViewController
, but in case 2 I present UINavigationController
and ViewController
is embed in
in this UINavigationController
.
Edit: I tried this code, but it always prints: "1........."
:
if self.presentingViewController != nil {
print("1..........")
} else if self.navigationController?.presentingViewController?.presentedViewController == self.navigationController {
return print("2.........")
} else if self.tabBarController?.presentingViewController is UITabBarController {
return print("3........")
}
And also this code prints:"Else.............."
:
let isPresentingInAddMealMode = presentedViewController is UINavigationController
if isPresentingInAddMealMode {
print("FirstOption......................")
} else {
print("Else......................")
}
If you need more info just let me know. Thank you very much.
I have solved it!! I put restorationIdentifier
to my root navigationController
and then I just check if that is navigationController
with my restorationIdentifier
like this:
if self.navigationController?.restorationIdentifier == "navViewController"{
let leftItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(goBack))
self.navigationItem.leftBarButtonItem = leftItem //Adds item if id is navViewController
}else{
self.navigationItem.leftBarButtonItem = nil //removes it and keeps "<Back"
}