How to remove border of the navigationBar in swift?
The trouble is with these two lines:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: ""), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage(named: "")
Since you don't have an image with no name, UIImage(named: "")
returns nil
, which means the default behavior kicks in:
When non-nil, a custom shadow image to show instead of the default shadow image. For a custom shadow to be shown, a custom background image must also be set with -setBackgroundImage:forBarMetrics: (if the default background image is used, the default shadow image will be used).
You need a truly empty image, so just initialize with UIImage()
:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
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!
SwiftUI Remove NavigationBar Bottom Border
In the initializer of your View you can set the appearance of your navigation bar. There you have to set the .shadowColor
property to .clear
.
init() {
let appearance = UINavigationBarAppearance()
appearance.shadowColor = .clear
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
How to remove navigation bar border/shadow?
Those two lines of code always do the trick for me :
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
Remove border for Navigation Bar and Toolbar
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
try this code I hope it'll solve your problem
How to remove bottom border of navigation bar with large title?
For removing the bottom border you only need to set:
navigationController?.navigationBar.shadowImage = UIImage()
With the below code you are affecting the background and that includes the status bar. Remove the line:
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
Swift How remove searchController bottom border?
One solution is to set the navigation bar's background and shadow images to an empty image.
i did one more change, just comment
navigationItem.largeTitleDisplayMode = .never
and Add these two line
navigationbar.setBackgroundImage(UIImage(), for: .default)
navigationbar.shadowImage = UIImage()
Here is the complete code :
if #available(iOS 11.0, *) {
let scb = searchController.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
scb.layer.masksToBounds = true
scb.layer.borderWidth = 10
scb.layer.borderColor = UIColor.clear.cgColor
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.layer.borderWidth = 2
textfield.layer.borderColor = UIColor.clear.cgColor
//textfield.textColor = // Set text color
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.borderWidth = 0
backgroundview.layer.borderColor = UIColor.clear.cgColor
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.white
navigationbar.setBackgroundImage(UIImage(), for: .default)
navigationbar.shadowImage = UIImage()
}
navigationItem.hidesSearchBarWhenScrolling = false
}
// navigationItem.largeTitleDisplayMode = .never
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: .bold) ]
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
definesPresentationContext = true
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.setBackgroundImage(UIImage(), for: .default)
navigationbar.shadowImage = UIImage()
}
}
Please check updated code.
Related Topics
How to Call Method from Viewcontroller in Gamescene
Are View Controllers with Nib Files Broken in iOS 8 Beta 5
Is Possible to Simulate Touch Event Using an External Keyboard on iOS Jailbroken
Is There Any Way of Asking an iOS View Which of Its Children Has First Responder Status
iOS Uialertview Button to Go to Setting App
Cookie Sharing Between Multiple Wkwebviews
iOS Is It a Static or a Dynamic Framework
Get Date and Time from Apple Server
Opengl Es 2.0 to Video on iPad/Iphone
Storyboard Segues Causing Memory Leaks
Uitextfield in Uialertcontroller (Border, Backgroundcolor)
Xcode 4.2: How to Import .H File from Subproject
Presenting Uiviewcontroller from Skscene
How to Get Hour and Minutes from Nsdate
iOS Horizontal Slideview with Vertical Menu
How to Get Server Response Data in Nsurlsession Without Completion Block