You can swipe up on the lines or hide them with Gesture hints. From Settings, tap Display, and then tap Navigation bar.
Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.
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()
Swift 4 & Swift 5
Removing border:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for:.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.layoutIfNeeded()
Restoring border:
self.navigationController?.navigationBar.setBackgroundImage(nil, for:.default)
self.navigationController?.navigationBar.shadowImage = nil
self.navigationController?.navigationBar.layoutIfNeeded()
With Swift 2 you can do it this way:
AppDelegate file
Inside func application(..., didFinishLaunchingWithOptions launchOptions:...)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
for Swift 3:
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
Just write this in the extension of UINavigationBar
extension UINavigationBar {
func shouldRemoveShadow(_ value: Bool) -> Void {
if value {
self.setValue(true, forKey: "hidesShadow")
} else {
self.setValue(false, forKey: "hidesShadow")
}
}
}
And in your viewController...
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.shouldRemoveShadow(true)
}
And to get this undone for any viewController, just pass false.
this will remove the shadow image altogether
for parent in self.navigationController!.navigationBar.subviews {
for childView in parent.subviews {
if(childView is UIImageView) {
childView.removeFromSuperview()
}
}
}
Swift 5
When using setBackgroundImage / shadowImage to hide the hairline, there's a slight delay. This method removes the delay. Credit to Chameleon Framework. This is the method they use (in ObjC)
extension UINavigationController {
func hideHairline() {
if let hairline = findHairlineImageViewUnder(navigationBar) {
hairline.isHidden = true
}
}
func restoreHairline() {
if let hairline = findHairlineImageViewUnder(navigationBar) {
hairline.isHidden = false
}
}
func findHairlineImageViewUnder(_ view: UIView) -> UIImageView? {
if view is UIImageView && view.bounds.size.height <= 1.0 {
return view as? UIImageView
}
for subview in view.subviews {
if let imageView = self.findHairlineImageViewUnder(subview) {
return imageView
}
}
return nil
}
}
Set barStyle
to .Black
before setting the tint:
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barStyle = .Black
self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With