How to Remove Border of the Navigationbar in Swift

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:

  1. Solid color, no translucency:

     navigationBar.barTintColor = UIColor.redColor()
    navigationBar.isTranslucent = false
    navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationBar.shadowImage = UIImage()
  2. Create small background image filled with color and use it.

  3. 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.

  1. Declare instance variable:

    private var shadowImageView: UIImageView?
  2. 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
    }
  3. 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



Leave a reply



Submit