I have a add button on my first view controller when I navigate to the second view controller the tab bar hides. I am using prepareForSegue method to navigate to second view controller. Here is my code of prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "AddDoc" {
let viewController = segue.destinationViewController as! UINavigationController
let destinationViewController = viewController.topViewController as! CategoryViewController
destinationViewController.doc = captureImageView.image
}
}
And I used the present modally segue to navigate. I have both my view controller embedded in navigation controller. Thank you!
Just set the dest view controller's hidesBottomBarWhenPushed property with true. destVC.hidesBottomBarWhenPushed = true
Related
In my iOS app, in a viewController I try to open a specific view in Tab Bar Controller.
I use a performSegue.
First I try to navigate straight to the specific view , but in this case the tab bar disappear
So I try to navigate to the tabViewController, and this lead me to the defult view (the first)
any idea how to navigate to a specific view in TabBarController ?
the performSegue I use:
self.performSegue(withIdentifier: "goToMain", sender: self)
//"goToMain" is my indentipier to the storyboard Segue
I use swift 4
with this code, you don't need segues, you can use when you push some button
let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "tabBarController") as! tabBarLoginViewController
VC1.selectedIndex = 2 //this line says that the view that appears will be third of you tab bar controller
self.navigationController!.pushViewController(VC1, animated: true)
if you want to use segue use this, the segue needs to point of tab bar controller, not a view of tab bar controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "goToMain") {
let vc = segue.destination as! TabBarController
vc.selectedIndex = 2
}
}
I created a table view and from there let say a user pressed a cell it will go to ListTavleView but the only problem right now is that whenever a user is in ListTableView there is not back button even thought i already embed a navigation controller
and i want the fist view navigation bar is small title second view navigation bar is large title
enter image description here
Below is my code
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showList" {
if let indexPath = tableView.indexPathForSelectedRow {
let items = dataManager.items[indexPath.row]
let controller = (segue.destination as! UINavigationController).topViewController as! ListTableViewController
controller.item = items
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
Below is my storybord setup
Navigation bar with no back button
From the image it seems that view controller is added as a child view controller in current view controller.
There is not need to embedded navigation controller when a cell is pressed becoz there is already a navigation controller at start point so no need to create a new one.(If you present a view controller then you may need to embed navigation controller.)
So the solution is...
Delete the navigation controller.
Connect directly to the destination view controller without navigation controller as there is already.
it is better if you use pushViewController, just get a reference of the other view controller, it will always a back button since you are pushing threw navigation Controller here is a simple example:
let story = UIStoryboard(name: "Main", bundle: nil)
let vc = story.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController
self.navigationController?.pushViewController(vc, animated: true)
as for the back button, the issue is with your hierarchy.
are you changing the left item of navigation bar in another view controller that might affect navigation bar in your destination view controller.
You are pushing new NavigationController(say Nav.B) to the existing one(Nav.A).
Each navigation controller keeps different navigation stack. The back button is visible when you add viewcontroller to Navigation controller. Read more about UINavigationController.
For your current scenario, you could delete the second navigation controller(i think it not essential) & connect direct segue to ListTableViewController
So this
let controller = (segue.destination as! UINavigationController).topViewController as! ListTableViewController
becomes
let controller = segue.destination as! ListTableViewController
When you need large titles(available 11+), you can add this line in viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
And if it needed only for this Viewcontroller, add in viewWillDisappear() or viewDidDisappear()
navigationController?.navigationBar.prefersLargeTitles = false
If you wanted to have navigation bar back button on next view, then just push the target view on navigation, it will show default navigation back button. No, need to any extra work.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showList" {
if let indexPath = tableView.indexPathForSelectedRow {
let items = dataManager.items[indexPath.row]
guard let controller = segue.destination as? ListTableViewController else {
return
}
controller.item = items
self.navigationController?.pushViewController(controller, animated: true)
}
}}
And if you are pushing the viewcontroller with segue, then no need to add below line self.navigationController?.pushViewController(controller, animated: true)
I am using a tab bar controller which holds 5 tabs. In tab1 I have a button which bring me to my tab2. This tab2 is embedded in a navigation controller.
So how do I make the Tab bar hidden in the tab2 when i come from tab1 by segue?
In storyboard I have a made Hide Bottom bar on push active. Also I have written self.tabBarController?.tabBar.isHidden = true in view did load of tab2.
In tab1 my prepareForSegue is like this
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShortcutSegue" {
let tabVc = segue.destination as! UITabBarController
tabVc.selectedIndex = 1
tabVc.tabBarController?.tabBar.isHidden = true
}
}
For the tab2 view controller, you can write the code below to hide the tab bar.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let destinationTabBar = segue.destinationViewController as? UITabBarController {
if segue.identifier == "ShortcutSegue" {
destinationTabBar.viewControllers?.removeAtIndex(adminScreenIndex)
}
}
}
Or you can write the code below in your override func prepareForSegue method.
if let tabVc = segue.destinationViewController as? tab2ViewController {
tabVc.hidesBottomBarWhenPushed = true
}
Or you can tick the "Hide Button Bar On Push" in the main storyboard in the attributes inspector for the tab bar view controller shown in the image below.
Main storyboard attribute inspector "Hide Button Bar On Push"
I have a few view controllers with segues presenting modally to a menu view controller. I also have a few buttons set up on the menu view. How do I perform a segue between the view controller (that is behind the modal) and a new view controller immediately after pressing a button on the menu and dismissing the modal? Thank you in advanced.
you can use prepareForSegue method between your view controllers
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "yourSegueIdentier" {
let indexPath = self.tableView.indexPathForSelectedRow as NSIndexPath!
//take your actions here
}
}
hope that helped
I just ported my project over to Swift 2, and everything is working great - except that even the most simple segues have no back button. Here is the prepare for segue function that I am using:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if segue.identifier == "showExercise" {
if let nav = segue.destinationViewController as? UINavigationController {
if let exercisesController = nav.topViewController as? ExercisesController {
let cell = sender as! WorkoutCell
if let workout = cell.name!.text {
exercisesController.exercises = Workouts[workout]!
exercisesController.navigationItem.title = workout
}
}
}
}
}
Before, the back button to the parent segue used to automatically populate. Now, all I get is the title in the child navigation vc
Are you using show or show detail segue? It seems like you are using a modal segue. Destination view controller for show or show segue is usually the second view controller itself, and not embedded in another UINavigationController.
If your destination view controller for the show segue is really a UINavigationController, the new navigation controller's navigation bar settings may override the old one (the navigation controller of the source view controller). Try not embedding your destination view controller in another UINavigationController.