iPhone hide Navigation Bar only on first page
The nicest solution I have found is to do the following in the first view controller.
Objective-C
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
Swift
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(true, animated: animated)
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(false, animated: animated)
super.viewWillDisappear(animated)
}
This will cause the navigation bar to animate in from the left (together with the next view) when you push the next UIViewController
on the stack, and animate away to the left (together with the old view), when you press the back button on the UINavigationBar
.
Please note also that these are not delegate methods, you are overriding UIViewController
's implementation of these methods, and according to the documentation you must call the super's implementation somewhere in your implementation.
How to hide a navigation bar from first ViewController in Swift?
If you know that all other views should have the bar visible, you could use viewWillDisappear
to set it to visible again.
In Swift:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
}
iOS: Hide the navigation bar for only one viewcontroller which is root of the UINavigationController?
I got the another way to hide/show navigationbar from one of my friend.
- Set a delegate for the NavigationController:
navigationController.delegate = self
- Hide/Show navigationbar for each ViewController all in one place
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let hide = (viewController is YourVC)
navigationController.setNavigationBarHidden(hide, animated: animated)
}
How to hide UINavigationBar from a UINavigationController?
This should work:
[self.navigationController setNavigationBarHidden:YES animated:YES];
to get it back, just call:
[self.navigationController setNavigationBarHidden:NO animated:YES];
source: How to hide the UINavigationBar for my first view
How to hide a navigation bar from one particular view controller
Try this method inside a view controller:
// swift
self.navigationController?.setNavigationBarHidden(true, animated: true)
// objective-c
[self.navigationController setNavigationBarHidden:YES animated:YES];
More clarifications:
UINavigationController
has a property navigationBarHidden, that allows you to hide/show the navigation bar for the whole nav controller.
Let's look at the next hierarchy:
--UINavigationController
------UIViewController1
------UIViewController2
------UIViewController3
Each of three UIViewController has the same nav bar since they are in the UINavigationController. For example, you want to hide the bar for the UIViewController2 (actually it doesn't matter in which one), then write in your UIViewController2:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES]; //it hides the bar
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES]; // it shows the bar back
}
How to hide UINavigationBar 1px bottom line
For iOS 13:
Use the .shadowColor
property
If this property is nil or contains the clear color, the bar displays no shadow
For instance:
let navigationBar = navigationController?.navigationBar
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.shadowColor = .clear
navigationBar?.scrollEdgeAppearance = navigationBarAppearance
For iOS 12 and below:
To do this, you should set a custom shadow image. But for the shadow image to be shown you also need to set a custom background image, quote from Apple's documentation:
For a custom shadow image to be shown, a custom background image must
also be set with the setBackgroundImage(_:for:) method. If the default
background image is used, then the default shadow image will be used
regardless of the value of this property.
So:
let navigationBar = navigationController!.navigationBar
navigationBar.setBackgroundImage(#imageLiteral(resourceName: "BarBackground"),
for: .default)
navigationBar.shadowImage = UIImage()
Above is the only "official" way to hide it. Unfortunately, it removes bar's translucency.
I don't want background image, just color##
You have those options:
Solid color, no translucency:
navigationBar.barTintColor = UIColor.redColor()
navigationBar.isTranslucent = false
navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.shadowImage = UIImage()Create small background image filled with color and use it.
Use 'hacky' method described below. It will also keep bar translucent.
How to keep bar translucent?##
To keep translucency you need another approach, it looks like a hack but works well. The shadow we're trying to remove is a hairline UIImageView
somewhere under UINavigationBar
. We can find it and hide/show it when needed.
Instructions below assume you need hairline hidden only in one controller of your UINavigationController
hierarchy.
Declare instance variable:
private var shadowImageView: UIImageView?
Add method which finds this shadow (hairline)
UIImageView:
private func findShadowImage(under view: UIView) -> UIImageView? {
if view is UIImageView && view.bounds.size.height <= 1 {
return (view as! UIImageView)
}
for subview in view.subviews {
if let imageView = findShadowImage(under: subview) {
return imageView
}
}
return nil
}Add/edit
viewWillAppear/viewWillDisappear
methods:override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if shadowImageView == nil {
shadowImageView = findShadowImage(under: navigationController!.navigationBar)
}
shadowImageView?.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
shadowImageView?.isHidden = false
}
The same method should also work for UISearchBar
hairline,
and (almost) anything else you need to hide :)
Many thanks to @Leo Natan for the original idea!
how to hide navigationbar when i push from navigation controller?
Put this code in the view controller you want to hide the navigation bar for.
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
And you may also want to stick this in there, depending on your needs:
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
Related Topics
Removing a View Controller from Memory When Instantiating a New View Controller
Swift Select All Photos from Specific Photos Album
Swift: Load Custom Uiview from Xib Programmatically
Unusernotificationcenter.Current().Getdeliverednotifications Only Ever Returns an Empty Array
iOS with Parse. Pfuser.Currentuser() Not Getting Cached. Returns Nil After App Restart
Make Skspritenode Subclass Using Swift
How to Send Msmessage in Messages Extension
Auto-Sizing Uicollectionview Headers
Why Protocol Is Better Than Class in Swift
Double Tap Necessary to Select Tableview Item with Search Bar
How to Convert Date (Without Knowing the Kind) to String
Uiview Rounded Corner - Swift 2.0
Cropping Avasset Video with Avfoundation Not Working iOS 8
How to Add Images as Text Attachment in Swift Using Nsattributedstring
Avmutablecomposition Resizing Issue
Swipe to Delete on a Tableview That Is Inside a Pageviewcontroller