I have three viewControllers:
FeedController (UITabBar is visible)
PostController (UITabBar is hidden)
UserController (UITabBar is visible
I do this with the following code, from FeedController to PostController:
let postVC = PostController()
postVC.hidesBottomBarWhenPushed = true
pushViewController(postVC, animated: true)
postVC.hidesBottomBarWhenPushed = false
Then, from PostVC to UserVC:
let userVC = UserController()
userVC.hidesBottomBarWhenPushed = false
pushViewController(userVC, animated: true)
It works great. It shows the UITabBar everywhere except when navigating to a Post. However, the problem occurs when I go to a User Profile (UserController) from within a Post. It shows the UITabBar on the profile, as intended, but when I navigate back (using the back button in my UINavigationController) the UITabBar is still visible. I want it to be hidden again when I go back from the userVC to the postVC.
Is there any way I can accomplish this?
try in your post viewController:
override func viewWillDisappear(_ animated: Bool) {
postVC.hidesBottomBarWhenPushed = true
}
That will call it when the view is about to disappear but not when it appears so it should hide when you go back.
Related
I have a tab bar which I only need on five screens. However, it goes to every screen when I push the view controller. How do I stop this from happening. I found a lot of solutions telling me to use hidesBottomBarWhenPushed but the problem I'm having with that is when I pop the view controller the tab bar is gone. How do I solve this problem? Also, please give me suggestions on my questions as I'm sort of new here! Thanks!
Edit: I've also seen this: self.tabBarController?.tabBar.hidden = false, but this kind of looks odd as the tab bar just disappears in the middle of the push animation.
Need to inherit UINavigationController custom navigation controller, override pushViewController & setViewControllers method to set hidesBottomBarWhenPushed, and then use custom navigation controller to jump
open class CustomNavigationController: UINavigationController {
...
open override func pushViewController(_ viewController: UIViewController, animated: Bool) {
if viewControllers.count > 0 {
viewController.hidesBottomBarWhenPushed = true
}
super.pushViewController(viewController, animated: animated)
}
open override func setViewControllers(_ viewControllers: [UIViewController], animated: Bool) {
if viewControllers.count > 1, let vc = viewControllers.last {
vc.hidesBottomBarWhenPushed = true
}
super.setViewControllers(viewControllers, animated: animated)
}
}
I actually figured it out. Basically in your viewdidAppear you add in self.hidesBottomBarWhenPushed = true and in your viewDidDisappear you add in
self.hidesBottomBarWhenPushed = false. Thanks for your guys's answers anyways.
I have a UILabel in my ViewController that has a NavigationController (let's say view controller A) with a tap gesture recognizer attached to the label. When the label is tapped another view appears (let's call it B). The user picks some text in B and the view dismisses back to A with the label text updated with the selection. So I created a delegation between A and B to get the selection. The problem is that I do not see the NavigationBar when B appears. Is there a way to fix this?
ViewController A
#IBOutlet weak var sectionName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let sectionLabelTap = UITapGestureRecognizer(target: self, action: #selector(labelTapped(_:)))
sectionName.isUserInteractionEnabled = true
sectionName.addGestureRecognizer(sectionLabelTap)
}
#objc func labelTapped(_ sender: UITapGestureRecognizer) {
let sectionNameVC = storyboard?.instantiateViewController(withIdentifier: "SectionName") as! SectionNameTableViewController
sectionNameVC.selectionNameDelegate = self
sectionNameVC.userData = userData
present(sectionNameVC, animated: true, completion: nil)
}
In order to display the Navigation bar the UIViewController needs to have a UINavigationController.
You can add that sectionNameVC ViewController into a UINavigationController to persevere the present animation.
In that case your code might look something like this:
#objc func labelTapped(_ sender: UITapGestureRecognizer) {
let sectionNameVC = storyboard?.instantiateViewController(withIdentifier: "SectionName") as! SectionNameTableViewController
sectionNameVC.selectionNameDelegate = self
sectionNameVC.userData = userData
let naviagtionController = UINavigationController(rootViewController: sectionNameVC)
present(naviagtionController, animated: true, completion: nil)
}
Or you can simply call pushViewController on the View Controller A's navigation Controller, like this:
self.navigationController?.pushViewController(sectionNameVC, animated: true)
This will add sectionNameVC into the View Controller A's navigation Controller stack. In this case the transition animation will be different, the sectionNameVC will come from your right.
You are missing the concept between "Presenting" View Controller & "Navigating" the View Controller. You will get the answer, once you understood the concept. Here, it is..
When you are presenting the ViewController, you are completely replacing the stack container to the new view controller.
STACK holds the addresses of the ViewControllers you push or pop via navigating.
e.g:
present(sectionNameVC, animated: true, completion: nil)
On the other hand, if you are navigating to other view controller by pushing it. In this case, you can go back to previous controller by simple popping the ViewController address from stack.
e.g:
self.navigationController?.pushViewController(sectionNameVC, animated: true)
self.navigationController?.popViewController(animated: true)
So, If you navigate then, only you will get navigation Bar.
Now, in your case, you are presenting the ViewController and hence, navigation bar is not showing.
If I have a UITabBarController and do this inside one of its view controllers:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tabBarController?.tabBar.isUserInteractionEnabled = false
}
the first time it shows, I can still change tabs.
If I tap on another tab and tap back on the first tab, then the tabbar is really disabled, but not the first time.
Why? And how do I solve this?
EDIT:
There is one detail I noticed, the tabBarController?.tabBar.isUserInteractionEnabled = false has to be on the second view controller of a navigation controller. In other words:
Say I have that structure
UITabBarController
UINavigationController
UIViewController (1)
UIViewController (2)
UIViewController (3)
So if I add that viewDidAppear code on view controller (2), you can change the tab once, but not the second time (after you navigate to it, obviously).
And there is more, if I go back after navigating to view controller (2), the tab bar becomes "interactable" again, without my setting it to true.
Having a tab bar in view, but not being able to interact with it will probably be confusing and frustrating for the user. And while I don't have the reason or solution for the original question, I have an alternative suggestion:
Hide the tab bar in UIViewController (2):
override func viewDidLoad() {
super.viewDidLoad()
tabBarController?.tabBar.isHidden = true
}
We're putting this in viewDidLoad so it's hidden as soon as the view
appears.
This also requires that you explicitly unhide it in UIViewController (1) for when the user hits the back button. Do it in viewWillAppear since the view was loaded already and we're going back to it.
override func viewWillAppear(_ animated: Bool) {
tabBarController?.tabBar.isHidden = false
}
While I have experienced the same behavior with:
tabBarController?.tabBar.isUserInteractionEnabled = false
..what you CAN do is disable each of the items in the tabBar.items collection, and then re-enable them in the viewWillAppear method of another controller. For example, if you didn't want your users tabbing out of menu #1 once they are inside it, you could do something like this in the subsequent controller(s):
override func viewWillAppear(_ animated: Bool) {
//0th tab items remains enabled as "only choice" for user
self.tabBarController!.tabBar.items![1].isEnabled = false
self.tabBarController!.tabBar.items![2].isEnabled = false
self.tabBarController!.tabBar.items![3].isEnabled = false
self.tabBarController!.tabBar.items![4].isEnabled = false
}
when the user tabs back to first tab (0) (or hits the back button), in that viewController's viewWillAppear method, re-enable the items:
override func viewWillAppear(_ animated: Bool) {
//re-enable tab items
self.tabBarController!.tabBar.items![1].isEnabled = true
self.tabBarController!.tabBar.items![2].isEnabled = true
self.tabBarController!.tabBar.items![3].isEnabled = true
self.tabBarController!.tabBar.items![4].isEnabled = true
}
I'm working with two table view controllers. When a button is pressed on a cell from table view a, table view b comes up on top of a.
In the code for tableview a I wrote that when the button that takes you to table b is pressed self.tabbarcontroller?.tabbar.hidden = true.
That worked for hiding the tabbar but when i put the code in the second table view self.tabbarcontroller?.tabbar.hidden = false the tabbar doesnt come back.
// present view controller
let vieww = self.storyboard?.instantiateViewControllerWithIdentifier("viewTable") as! viewsVC
vieww.view.backgroundColor = .lightGrayColor()
vieww.view.alpha = 0.9
vieww.modalPresentationStyle = .OverCurrentContext
vieww.hidesBottomBarWhenPushed = true
self.presentViewController(vieww, animated: true, completion: nil)
// hide tabbar
self.tabBarController?.tabBar.hidden = true
Add the code
self.tabBarController?.tabBar.hidden = false
to the ViewDidAppear method, rather than the viewDidLoad method in the original tableViewController.
The issue you are encountering is that the view is not loaded after you go back, but rather appears right away since it has already been loaded beforehand.
It should look like this:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.tabBarController?.tabBar.hidden = false
}
This should fix the problem.
Good luck with your project!
I have read about similar problems where the problem was caused by having multiple navigation controllers, but I only have one. This is my navigation flow.
VC = UIViewController, NC = UINavigationController
VC1 -modal-> NC -root-> VC2 -show-> VC3
VC1 is not embedded in a navigation controller, I'm starting that modal segue using performSegueWithIdentifier:sender:.
VC2 then is using a show segue to present VC3, which is the one where the back button is not visible. It still works though. But, it does appear if I exit to the home screen and then enters the app again, as shown here:
https://gfycat.com/VelvetyThisHamster.
Any ideas why this is happening?
edit: To make things clear: I want the button both visible and functioning (it's not that it's working that's the problem, but that it's hidden)
EDIT 2:
If I change my navigation flow to this
NC -root-> VC2 -show-> VC3
then the back button works as intended. So the question is, how can I add a regular view controller without a navigation controller before the first navigation controller? I want it before because VC1 should not have a navigation bar and VC2 should be presented modally.
try this
Hidden
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//use this
self.navigationItem.setHidesBackButton(true, animated: false)
//else use this
self.navigationItem.leftBarButtonItem = nil
}
Show
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//use this
self.navigationItem.setHidesBackButton(false, animated: false)
//else
self.navigationController.navigationItem.backBarButtonItem.enabled = TRUE
}
Update
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//use this
self.navigationItem.setHidesBackButton(false, animated: false)
//else
let backButton = UIBarButtonItem(title: "leftbutton", style: UIBarButtonItemStyle.Plain, target: self, action: "buttonMethod")
self.navigationItem.leftBarButtonItem = backButton
}
func buttonMethod() {
print("Perform action")
}
I think I found the source of the problem, so I'll post it here in case someone else runs into the same problem.
The modal presentation between VC1 and NC was made from a background queue (by calling performSegueWithIdentifier:sender: in the completion handler of an NSURLSessionDataTask to be precise). By dispatching that line of code to the main queue the problem seems to disappear.
Turn out, I had NavigationBar tint color set to "Clear". Once I changed it, the back button appeared.