Hide Navigation Bar in Specific View - Swift 3 - ios

I have NavigationController that handles navigation through my app.
According to my design, the very first view should have no visible NavigationBar. All the others after, will.
In this FirstView, I'm using this so far to hide the NavBar, inside the ViewDidLoad:
self.navigationController?.isNavigationBarHidden = true
From this FirstView I can access other Views. In these other views I show the NavBar using:
self.navigationController?.isNavigationBarHidden = false
My problem is that:
When I navigate from a View with Visible NavBar, back to the FirstView with the Hidden NavBar, the NavBar is now visible.
Basically the NavBar only hides the very first time then shows if I use the back button.
How Can I Prevent this ?
Thank you!

Move that code to viewWillAppear() instead of viewDidLoad().
viewDidLoad() is only called once per instantiated view controller, whereas viewWillAppear() is called whenever the view controller is about to be presented on screen.
You can read more about the view controller lifecycle here.

Write below code in your FirstViewController's viewWillAppear method.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.isNavigationBarHidden = true
}
And in your SecondViewController's viewWillAppear method write below code
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.isNavigationBarHidden = false
}
Do not try to hide and show nav bar in viewWillAppear and viewWillDisappear subsequetly in FirstViewController.

You can use this function to hide NavigationBar with cool animation:
func setupAnimationForNavigationBar(caseOfFunction: Bool) {
if caseOfFunction == true {
UIView.animate(withDuration: 0.5) {
self.navigationController?.navigationBar.transform = CGAffineTransform(translationX: 0, y: -200)
}
} else {
UIView.animate(withDuration: 0.5, animations: {
self.navigationController?.navigationBar.transform = CGAffineTransform.identity
})
}
}
If you want to hide NavigationBar, so set it "True" and if you want to call NavigationBar again, set it "False"

Related

How do you hide/unhide the tab bar when pushing/popping

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.

Hide and show navigation bar for specific view without laggy animation

Is there a way to show navigation view on one view and not show it on another at the same time?
The problem: I have two view controllers - table and description view (called on cell click).
Table got a navigation bar, while description view - don't have it.
Table view:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.isNavigationBarHidden = false
}
Description view controller:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
Everything works fine, but when i swipe for half screen back to table (keeping finger on screen, watching both views) - i don't see navigation bar (which works as expected with that code), and when i release finger - whole table view jumps, because nav bar is shown.
Is there a way to keep not seeing nav bar in description view and see it all the time in table view?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
You can hide the navigation bar while doing the segue (earlier). If you're doing it programmatically:
yourVCToBePushed.navigationController?.isNavigationBarHidden = true
If you're doing it in the storyboard, do similarly inside prepareForSegue:
let yourVCToBePushed = segue.destination as! YourVCToBePushed (type)
yourVCToBePushed.navigationController?.isNavigationBarHidden = true
You can also create your own "navigationView" inside tableView header, and add buttons there.

Setting isUserInteractionEnabled on tabBar doesn't work the first time

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
}

Navigation bar not showing iOS swift

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)

Hide tab bar in IOS swift app

I'm trying to figure out how to hide the tab bar in my iOS swift app. I don't care about any fancy animations or anything. Just something I can put in the ViewDidLoad() function.
You can simply use this in your ViewDidLoad() method.
self.tabBarController?.tabBar.hidden = true
For Swift 3.0, 4.0, 5.0:
self.tabBarController?.tabBar.isHidden = true
Or you can change z position of tab bar this way:
self.tabBarController?.tabBar.layer.zPosition = -1
and if you want to show it again then:
self.tabBarController?.tabBar.layer.zPosition = 0
The accepted answer works, but the transition to other view has a choppy animation (The tab Bar animation)
Also wanted to add although Kalpesh's solution worked perfectly for me, I found out that every view controller has an attribute for hidesBottomBarWhenPushed (check out storyboard.) If you wish to hide tab bar, you should put a tick on that. And it would work great.
Update: Im not sure if this is a known thing, but here's what apple documentation page says:
A view controller added as a child of a navigation controller can
display an optional toolbar at the bottom of the screen. The value of
this property on the topmost view controller determines whether the
toolbar is visible. If the value of this property is true, the toolbar
is hidden. If the value of this property is false, the bar is
visible.
I think this means that you have to set the basic value of hidesBottomBarWhenPushed at the topmost view controller (the first one on the navigation stack.) Once you have set that to true, you can change to false or true for the other viewcontrollers on the stack. But, if your topmost view controller's hidesBottomBarWhenPushed value is false, it will not show a tab bar for other controllers on the navigation stack.
Before push set controller.hidesBottomBarWhenPushed = true
let objCreateEventVC = CreateEventVC()
objCreateEventVC.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(objCreateEventVC, animated: false)
No need to set tabBar's isHidden property.
Simply, Go to ViewController (in StoryBoard) -> Attribute inspector -> Under 'View Controller' section select 'Hide Bottom Bar on Push' checkbox. This works like a charm.
If you go the 'isHidden' way you need to do a lot of handling, i.e. to make it appear again when you go back and also to remove the bottom empty space after hiding tabBar.
Swift 3.
self.tabBarController?.tabBar.isHidden = true
You can also set it in extension (use Dharmesh Kheni answer)
extension UITabBar {
func tabsVisiblty(_ isVisiblty: Bool = true){
if isVisiblty {
self.isHidden = false
self.layer.zPosition = 0
} else {
self.isHidden = true
self.layer.zPosition = -1
}
}
This is the way programmatically for Swift 4.0, 4.1, 4.2, 5.0 and later >:
tabBarController?.hidesBottomBarWhenPushed = true
or
hidesBottomBarWhenPushed = true
Hide & Show Tab Bar With Animation
For those looking to hide/show the tab bar with animation.
Since iOS 13, the behavior of the UITabBar has changed for animations. You can no longer use CGAffineTransform and instead you should animate its frame position.
See my full guide here: hide & show tab bar with animation
To hide the tab bar:
UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.7, options: .curveEaseOut) {
if let tabBarFrame = self.tabBarController?.tabBar.frame {
self.tabBarController?.tabBar.frame.origin.y = navigationController.view.frame.maxY + tabBarFrame.height
}
navigationController.view.layoutIfNeeded()
} completion: { _ in
self.tabBarController?.tabBar.isHidden = true
}
To show the tab bar:
UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.7, options: .curveEaseOut) {
if let tabBarFrame = self.tabBarController?.tabBar.frame {
self.tabBarController?.tabBar.frame.origin.y = navigationController.view.frame.maxY - tabBarFrame.height
}
navigationController.view.layoutIfNeeded()
}
Make sure to call layoutIfNeeded() inside the UIView.animate to trigger the changes with animation.
To hide the navigationBar and the tabBar I use the next function:
var tabBarHeight : CGFloat!
func fullScreenAction(){
if navigationController?.isNavigationBarHidden ?? false {
//Show navigationBar
navigationController?.setNavigationBarHidden(false, animated: false)
//Show tabBar
tabBarController?.tabBar.isHidden = false
//Update the height of tabBar
if (!(tabBarController?.tabBar.frame.size.height.isEqual(to: 0))!) {
tabBarHeight = self.tabBarController?.tabBar.frame.size.height
}
tabBarController?.tabBar.frame.size.height = tabBarHeight
} else {
//Hide navigationBar
navigationController?.setNavigationBarHidden(true, animated: false)
//Hide tabBar
tabBarController?.tabBar.isHidden = true
//Update the height of tabBar
tabBarHeight = tabBarController?.tabBar.frame.size.height
tabBarController?.tabBar.frame.size.height = 0
}
}
When the screen orientation has changed the height of tabBar change too, so I use the next function to exit of fullscreen to resize the height:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if navigationController?.isNavigationBarHidden ?? false {
navigationController?.setNavigationBarHidden(false, animated: false)
tabBarController?.tabBar.isHidden = false
}
}
I hope it is useful for you.
Here is my code. it's just to hide its tabbar. (If no frames are well established there will be a black view at the bottom. )
var oldTabbarFr: CGRect = .zero
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
oldTabbarFr = self.tabBarController?.tabBar.frame ?? .zero
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = true
self.tabBarController?.tabBar.frame = .zero
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.tabBarController?.tabBar.isHidden = false
self.tabBarController?.tabBar.frame = oldTabbarFr
}

Resources