How to "virtually" tap back button in Navigation Bar - ios

I have app, where is Segmented Control inside of my Navigation Bar. Under navigation bar I have 3 containers. In these containers I have Table View Controllers. If you tap on segmented control, one TVC appear and others disappear (container1.hidden = true and so on).
Problem is when I press "save" button which is also in navigation controller - button doesn't trigger "virtual push of back button".
I used following code which works in my other projects (its in button's action which is in VC that contains all container views) but not this time:
if let navController = self.navigationController {
navController.popViewControllerAnimated(true)
}
Image for better insight:
UPDATE: Thanks to # Alexey Bondarchuk I solved it. Comments may be confusing so I just recap problem and solution.
Originally, I had ViewController. To this controller I embed in Navigation Controller. To this Navigation Controller I connected segues. And that was mistake.
So I deleted this embed in navigation controller, made (show) segues directly to my View Controller (which is on screenshot). This automatically created navigation bar and last thing I did was that I put navigation item in it so now my code pop right navigationController. Hope that it's understandable.

I have couple ideas:
Your navigationController equal to 'nil' and .popViewControllerAnimated will never invoked. This may happen if you are using UITabBarController. In this case try to use self.tabBarController?.navigationController instead of self.navigationController.
Your controller presented 'Modally'. In this case you can try to invoke navController.dismissViewControllerAnimatedinstead of navController.popViewControllerAnimated

Related

Swift: how to make tab view controller button not show?

Ok, I have all my view controllers managed by a tab bar controller, and obviously when you connect in the storyboard the relationship between the controller and the Vcs as "view controller" a button appears on the tab bar. These are then accessible via the tab bar controller.
My problem is I need to have one VC be the in view controller array but NOT BE SHOWN IN A BUTTON on the tab bar. It should not be accessible to the user, but I need to be able to switch to that VC in code with selectedIndex
I do not know how to do this. I have tried disabling the button and doing
self.tabBar.items?[3].accessibilityElementsHidden = true
But the button is still there on the tab bar. How can I have the VC in the array of the tab bar controller but not shown in a button?
This is not possible using the UITabBarController as is.
Not 100% sure on why you need to have a VC in that array but never visible on the bar, consider subclassing UITabBarController if thats the case.
Changes to the viewControllers or items property of a UITabBarController will encourage iOS to reload the Component and hence why you are seeing the VC in the TabBar.

Toolbar not visible

Can anyone tell me why, in the storyboard here below, when I arrive from the first tab the toolbar is visible, but when I arrive from the second tab is not visible anymore.
The first tab has a navigation controller as the root controller - the second does not.
You can select SearchVC in your storyboard and then use the Editor - Embed In - Navigation Controller menu option to add a navigation controller. That might not be how you want your UI. But that would achieve what you want visually ...
Update:
Based on discussions with the OP, the issue was that SearchVC was adding EditorVC as a child view controller and then EditorVC in turn would add ReadVC to the navigation stack.
EditorVC, as it appears above, is embedded within a UINavigationController. I suggested that OP remove that UINavigationController instance and then push EditorVC on to SearchVC's navigation controller and then later when ReadVC is displayed from EditorVC, again push ReadVC on to the existing navigation controller. I believe this should resolve the issues that OP was seeing.

TabBar disappear after Push Segue

This is the current layout for my application. As you can see, I have a ViewController that is embedded in a TabBarViewController. You can see I have two tab bars in both of those bottom view controllers but only the first one shows up. In the second view controller after the push segue, the tab bar disappears. Why is this?
I added the properties for the First view controller and it is not set to hide the bottom bar during the segue so I am confused as to why it would disappear after the segue. Any ideas?
You'll need to wrap your tabBar's root viewControllers in a UINavigationController. So your UITabBarController would actually be pointed at the Navigation Controller. Then as you move around in that navigation controller, the tab bar will stay in place.
To fix this in your application, select your view controller in storyboard, then click "Editor" -> "Embed In" -> "Navigation Controller".
Here's a visual representation I just threw together for anyone else who comes across this problem. If you remove the "NavigationController" in the storyboard shown below, the tab will disappear when you click the button in "First View". With the navigation controller, you will maintain the tab bar. Hope this helps.
Try set self.tabBarController.tabBar.translucent = NO; in viewWillAppear
You could also try to dismiss the views by adding an outlet/action. For example, I experienced an issue where I had a TabBar view controller and needed to segue between 2 different views (ImageViews) on one of the tabs and as soon as I did a traditional segue, the whole tab bar disappeared. I had created the following "Back button" to clear the view:
#IBAction func backBtnPressed(_ sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
Note: It is an important practice to clear views out as they will stack up overtime and will reduce the performance of your app.
Technical Info:
https://developer.apple.com/reference/uikit/uiviewcontroller/1621505-dismiss
Not sure if this helps but worth mentioning!

Why is the tab bar disappearing?

My current setup of viewcontrollers are:
tab view > navigation controller > table view controller > navigation view controller > cell details. Please see
The current setup of viewcontrollers
I used to have:
tab view > navigation controller > table view controller > cell details
and then everything was fine.
The issue is that I need a custom action to happen when the user presses the back button, and to do this i added a nav controller between the "table view" and the "cell details". And thats when the tab bar disappeared. I understand this seems to be "normal" behaviour, but that donĀ“t help me much. Please help.
The code that segues to the detail view controller. (I use the storyboard, so light on code for these things)
#IBAction func add(sender: AnyObject) {
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("TableViewToDetailView", sender: self)
}
It happens when hideTabBarOnPush property is true(in code) or Hide Bottom Bar on Push is checked on storyboard for the controller you are pushing.
I had a similar problem and the right question was kind of hard to ask. using Tsb Bar Controllers with Navigation Controllers and View Controllers os too tricky and certain things a re not allowed and there is a lot of terminology, and, there are many different types of segues,and there are many different kinds of consequences for doing certain things.
I found the correct procedure (answer) in the second part of this two part series:
Storyboards Tutorial for iOS: Part 1
Storyboards Tutorial for iOS: Part 2
Summary of the procedure:
Embed the source and destination View Controllers in Navigation Controllers, then create unwind segues (methods with the signature#IBAction func methodname(_ segue:)) in the source view controller. Then, from the destination View Controller, control-click from the navigation bar button (or any view required to trigger a return to the first view controller) to the Exit object above the view controller then pick the correct action name from the popup menu. The unwind segue will be accessible from the Document Outline and can be given an identifier to use in prepare(for:sender:) in case data needs to be sent to the from the destination view controller. Also, the segue from the first view Controller to the second navigation controller must be modal.
A similar issue I met although maybe not quite the same as yours but hope it may help. When view controller A presents view controller B, the hidesBottomBarWhenPushed property of B could be overridden by the hidesBottomBarWhenPushed property of A. I fixed it by setting B's modal style.

Different navigation item for each view in tab bar controller

The Problem
I'm relatively new to Swift and I'm trying to build an application that makes use of a UITabBarController.
What I'm trying to do is put a different navigation bar (or UINavigationItem) on each of the tabs in the UITabBarController.
For example, I want the UINavigationItem I set, with its bar button items, to appear on MyViewController instead of a back button to the previous view controller, such as shown on the image below.
The current layout on the Storyboard is as follows.
MyViewController on the sidebar:
What I've Tried
Someone suggested that I should embed each UIViewController (e.g. MyViewController) in a Navigation Controller. I've tried this and it doesn't work.
I've also tried to set the Top Bar to "None" in the Attributes tab of the options menu.
Thank you in advance for your help.
Here is how i did it,
UINavigationController -> UITabbarController
And then each "Tab" is in different Storyboard and every storyboard start with a "Navigation Controller". So yes every tab in different navigation controller this how you should do it.
Different storyboards because may be multiple people work on storyboard at same time.
Why TabbarController inside Navigationcontroller ?
I put the "TabbarController" inside "NavigationController" because some of the controllers i want them to be full screen, like hiding the "Tabbar" so for that i push them from main NavigationController.
//Out of context but may help you,
I have created an "Extension" of Navigation Controller to push a view controller on main navigation so that any of the tabbars (which are also inside navigation controllers) can easily use the extension to push any view controller if want to hide the tabbar.

Resources