我有一个应用程序,有时需要它的导航栏融入内容。

有人知道怎么去掉或者改变这个烦人的小条的颜色吗?

在下图中,我说的是根视图控制器下面1px的高度线


当前回答

pxpgraphics对Swift 3.0的回答。

import Foundation
import UIKit

extension UINavigationBar {

  func hideBottomHairline() {
    let navigationBarImageView = hairlineImageViewInNavigationBar(view: self)
    navigationBarImageView!.isHidden = true
  }

  func showBottomHairline() {
    let navigationBarImageView = hairlineImageViewInNavigationBar(view: self)
    navigationBarImageView!.isHidden = false
  }

  private func hairlineImageViewInNavigationBar(view: UIView) -> UIImageView? {
    if view is UIImageView && view.bounds.height <= 1.0 {
      return (view as! UIImageView)
    }

    let subviews = (view.subviews as [UIView])
    for subview: UIView in subviews {
      if let imageView: UIImageView = hairlineImageViewInNavigationBar(view: subview) {
        return imageView
      }
    }
    return nil
  }
}

extension UIToolbar {

  func hideHairline() {
    let navigationBarImageView = hairlineImageViewInToolbar(view: self)
    navigationBarImageView!.isHidden = true
  }

  func showHairline() {
    let navigationBarImageView = hairlineImageViewInToolbar(view: self)
    navigationBarImageView!.isHidden = false
  }

  private func hairlineImageViewInToolbar(view: UIView) -> UIImageView? {
    if view is UIImageView && view.bounds.height <= 1.0 {
      return (view as! UIImageView)
    }

    let subviews = (view.subviews as [UIView])
    for subview: UIView in subviews {
      if let imageView: UIImageView = hairlineImageViewInToolbar(view: subview) {
        return imageView
      }
    }
    return nil
  }
}

其他回答

对于iOS 13:

使用.shadowColor属性

如果此属性为nil或包含透明颜色,则该栏不显示阴影

例如:

let navigationBar = navigationController?.navigationBar
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.shadowColor = .clear
navigationBar?.scrollEdgeAppearance = navigationBarAppearance

对于iOS 12及以下版本:

为此,您应该设置一个自定义阴影图像。但是为了显示阴影图像,你还需要设置一个自定义背景图像,引用自苹果的文档:

要显示自定义阴影图像,必须有自定义背景图像 也可以使用setBackgroundImage(_:for:)方法进行设置。如果默认 使用背景图像,然后将使用默认阴影图像 不管这个属性的值。

So:

let navigationBar = navigationController!.navigationBar
navigationBar.setBackgroundImage(#imageLiteral(resourceName: "BarBackground"),
                                                        for: .default)
navigationBar.shadowImage = UIImage()

以上是唯一的“官方”隐藏方法。不幸的是,它消除了杆的半透明。

我不需要背景图像,只是颜色##

你有这些选择:

纯色,无半透明: navigationBar。barTintColor = uiccolor . redcolor () navigationBar。isstranslucent = false navigationBar.setBackgroundImage(UIImage(), for: .default) navigationBar。shadowImage = UIImage() 创建小背景图像充满颜色和使用它。 使用下面描述的“hack”方法。它也将保持酒吧半透明。

如何保持酒吧的半透明?# #

为了保持半透明,你需要另一种方法,它看起来像一个黑客,但工作得很好。我们试图移除的阴影是UINavigationBar下面的一个发际线UIImageView。我们可以找到它,并在需要时隐藏/显示它。

下面的说明假设你只需要在UINavigationController层次结构的一个控制器中隐藏发际线。

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 }

同样的方法也适用于UISearchBar发际线, 以及(几乎)任何你需要隐藏的东西:)

非常感谢@Leo Natan的原创想法!

这里有一个非常简单的解决方案:

self.navigationController.navigationBar.clipsToBounds = YES;

适用于iOS 13+

窍门是用透明背景初始化'UINavigationBarAppearance'。然后,您可以轻松地删除导航栏的水平线。

let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundColor = .green // Required background color

最后,按照apple的建议将外观更改添加到导航项中。

self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
self.navigationItem.compactAppearance = appearance

斯威夫特说

UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: .Any, barMetrics: .Default)
UINavigationBar.appearance().shadowImage = UIImage()

in

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool

简单的解决方案- Swift 5

Create an extension: extension UIImage { class func hideNavBarLine(color: UIColor) -> UIImage? { let rect = CGRect(x: 0, y: 0, width: 1, height: 1) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() context?.setFillColor(color.cgColor) context?.fill(rect) let navBarLine = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return navBarLine } } Add this to viewDidLoad(): self.navigationController?.navigationBar.shadowImage = UIImage.hideNavBarLine(color: UIColor.clear)