I have embedded a viewController in a NavigationController and set it as the rootViewController. Then I connected the TabBarController to the NavigationController. I have a button in the LessonViewController that shows the PurchaseViewController, and then a back button in the PurchaseViewController which shows the LessonViewController. However, the tab bar was still present in the PurchaseViewController so I ticked hideBottomBarOnPush, which solved this problem, however, when I segued back to the LessonViewController the tab bar had disappeared.
Any ideas?
The following image is what my storyboard looks like now:
Similar to barb’s code, I got this to work, while enabling “hide bottom toolbar when pushed” and then popping the view controller:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden = false
}
You should do following way,
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Your_Identifier" {
hidesBottomBarWhenPushed = true
DispatchQueue.main.async { self.hidesBottomBarWhenPushed = false }
}
}
It will show TabBar reappears while segue back.
Related
I'm segueing to a view linked with a UINavigationController, but the UINavigationBar doesn't appear. There is no sign of the UINavigationController whatsoever.
My StoryBoard looks like this:
To perform the segue, I'm using this:
self?.performSegue(withIdentifier: "practicemod", sender: self)
The preparation function looks like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var vc = segue.destination as! PracticesView
vc.practices = sdkPractices
vc.consumer = sdkConsumer
}
Can someone help me out here? What am I missing?
The start point in your storyboard need to be Navigation Controller.
OR
segue from select view to Navigation Controller to TableView Controller (PracticesView).
This should solve your problem
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 following View Controller hierarchy in my storyboard.
When I perform show segue from tab 1 of Groups Tab bar Controller by selecting the tableview cell, the Lists tab bar is presented but it have Groups's tab bar instead of its own tab bar.
How can I make it to show it's own tab bar when Lists's Controller is shown. Thanks
PS: I have one more tab bar controller in the hierarchy.
Make sure you set the Storyboard ID in interface builder for "List Tab Bar Controller." (Example below tabBar2)
Give a name for the segue between the TableViewController and ListTabBarController. (Example below show1)
Add the following code to prepare (for segue):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "show1" {
let _ = navigationController?.popViewController(animated: true)
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let tabVC2 = storyborad.instantiateViewController(withIdentifier: "tabBar2") as! UITabBarController
self.present(tabVC2, animated: true, completion: nil)
}
}
Adding this code
override func viewWillAppear(_ animated: Bool) {
self.hidesBottomBarWhenPushed = true
}
override func viewWillDisappear(_ animated: Bool) {
self.hidesBottomBarWhenPushed = false
}
in Table View Controller of both Tab bar Controllers worked. Now every Tab bar Controller's View Controllers have their own tab bar.
Situation:
I have a few ViewControllers (all with a NavigationBar) embedded in a TabBarController. I have one specific ViewController (VC1) where I don't want to show the TabBar. From there you can go to another specific ViewController (VC2), where the TabBar needs to be shown again.
My solution:
VC1
self.hidesBottomBarWhenPushed is set to true by default
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
self.hidesBottomBarWhenPushed = false
}
override func viewWillDisappear(animated: Bool) {
self.hidesBottomBarWhenPushed = true
}
VC2
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
self.hidesBottomBarWhenPushed = true
}
override func viewWillDisappear(animated: Bool) {
self.hidesBottomBarWhenPushed = false
}
So far so good. This seems to be working, but you can push another VC1 from VC2 (Same controller with different content) and of course push another VC2 from VC1 again and so on.
The Problem:
As soon as VC2 is pushed twice from a VC1 the TabBar is always hidden.
When you hit the back button to go from a VC1 back to a VC2 the TabBar is always hidden.
Customize only VC1
override func viewWillAppear(animated: Bool) {
self.tabBarController?.tabBar.hidden = true
}
override func viewWillDisappear(animated: Bool) {
self.tabBarController?.tabBar.hidden = false
}
It is simpler architecture
Do not put the logic in viewWillDisappear or prepareForSegue since you do not know what kind of behavior the view controller that is about to be presented wants. Put the logic inside viewWillAppear instead.
Let every ViewController handle its own desired behavior and do not try to anticipate what the destination wants. Especially because you do not always know what the reason for viewWillDisappear or prepareForSegue is - therefore you cannot react accordingly.
Try change property in needed VC:
self.navigationController.toolbarHidden = YES;
Try these code in viewWillApear for hide or unhide , it'll work fine .
For VC1 : - In this you want always hide then add this code
override func viewWillAppear(animated: Bool) {
self.tabBarController?.tabBar.hidden = true
}
For VC2 : - In this you want always show then add this code
override func viewWillAppear(animated: Bool) {
self.tabBarController?.tabBar.hidden = false
}
Try this code , Its working fine . I also tried this code in sample project.
I am having multiple view controller in my application. I want to hide navigationbar in my first view controller. So I use the following code to hide the navigation bar
navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true);
Now I want to add navigation bar in some other viewController but, my navigation bar not visible in that viewcontroller. Why it is happening?
My storyboard showing the navigation bar but once I try to run my application it is gone.
If I hide navigation bar from one view controller then we can't use navigation controller, Is it so? I hope I am wrong. Then what are the reasons for navigation bar not shown?
EDIT:
Also I want my view controller in portrait mode only. So I did the following Is that causing the issue?
extension UINavigationController{
public override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return false
}
else {
return true
}
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown]
}
}
Edit 1:
I am using following code to move from one view controller not link from the storyboard. Is that causing issue now?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
presentViewController(secondViewController, animated: false, completion: nil)
Edit 2:
Please check my following screenshots. Which are my settings for secondview controller
Edit 3:
Here is my navigation controller attribute inspector
Navigation Controller is a controller, which has stack of view controllers. So if you have something like this:
NAV -> A -> (segue) B
Even if you'll hide navigation bar you still should be able to make segues. Also can't you just unhide navigation bar in second (B) view controller in viewWillAppear? And in first the same way hide it on viewWillAppear.
edit: Final solution to the problem:
Use:
let controller = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
self.navigationController!.pushViewController(controller)
instead of:
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
presentViewController(secondViewController, animated: false, completion: nil)
Because pushViewController will add secondViewController to its stack. presentViewController was replacing your navigation controller that's why you couldn't see navigation bar.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Hide the navigation bar on the this view controller
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Show the navigation bar on other view controllers
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
in viewDidLoad method of the view controller in which you don't want to show navigation bar add the line
navigationController.navigationBarHidden = true
you are presently hiding in all view controllers
Edit: You are presenting view controller instead it should be
self.navigationController!.pushViewController(controller)
do like in viewcontroller based hidden using Swift 4.0
To hide navigationController in viewWillAppear
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
To unhide navigationController in viewWillDisappear
override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = false
}
I am having same requirement in my swift project.
this is how I have handled Navigation bar
Make sure your first screen is embedded into Navigation controller
example we have two screens A and B
In screen A you need to hide navigation bar in viewWillAppear
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//hide navigation for screen A
self.navigationController?.navigationBarHidden = true
}
for enabling Navigation in screen B
you need to add below code in screen A
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
{
if (segue.identifier == "screen B's segue identifier here")
{
//enable navigation for screen B
navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true)
}
}
Using above style, I can enable or disable navigation bar for specific screen, whenever I want
If you need to have this navigation bar hidden only in this controller, the best way is to show it in viewWillDisappear() and hide in viewWillAppear().
It's too late to reply and there are other good answers but I would like to share what worked for me.
let controller = self.storyboard?.instantiateViewControllerWithIdentifier("HomeVC")
self.navigationController!.pushViewController(controller!, animated:true)