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

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

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


当前回答

我刚刚为这个创建了一个扩展…关于格式的问题很抱歉(这是我的第一个答案)。

用法:

  override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.hideShadow = true
}

扩展:

 UINavigationController.swift
//  Created by Ricardo López Rey on 16/7/15.

import Foundation


struct UINavigationControllerExtension {
    static var hideShadowKey : String = "HideShadow"
static let backColor = UIColor(red: 247/255, green: 247/255, blue: 248/255, alpha: 1.0)
}

extension UINavigationController {

    var hideShadow : Bool {
        get {
            if let ret =  objc_getAssociatedObject(self, &UINavigationControllerExtension.hideShadowKey) as? Bool {
                return ret
            } else {
                return false
            }


        }
        set {
            objc_setAssociatedObject(self,&UINavigationControllerExtension.hideShadowKey,newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))

            if newValue {


            self.navigationBar.setBackgroundImage(solidImage(UINavigationControllerExtension.backColor), forBarMetrics: UIBarMetrics.Default)

                self.navigationBar.shadowImage = solidImage(UIColor.clearColor())
            } else {
                self.navigationBar.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default)
            }
        }
    }

    private func solidImage(color: UIColor, size: CGSize = CGSize(width: 1,height: 1)) -> UIImage {
        var rect = CGRectMake(0, 0, size.width, size.height)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        color.setFill()
        UIRectFill(rect)
        var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }


}

其他回答

这里有一个非常重要的注意事项——改变UIViewController的navigationItem的外观比直接改变navigationBar要灵活得多。

为什么这么问?

For the simple reason that the navigationItem is tied to a single UIViewController and represents the state of the navigationBar for that particular UIViewController. This is big, as you don't have to handle the navigation bar changes between different view controllers within viewWillAppear (or something similar), as you would if you mutated the navigationBar; which is, remember, shared between all view controllers of a given navigation stack (UINavigationController), and changing it in one place changes it for all view controllers up to the stack.

你只要为一个特定的视图控制器设置正确的UINavigationBarAppearance, UIKit就会正确地更新导航栏样式,这取决于当前哪个视图控制器是导航堆栈上的顶视图控制器。

navigationItem.standardAppearance` = `UINavigationBarAppearance()

对于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的原创想法!

我的方法:

UINavigationBar.appearance().setBackgroundImage(
            UIImage(),
            forBarPosition: .Any,
            barMetrics: .Default)
    var _width:CGFloat! = self.navigationController?.navigationBar.layer.frame.width
            var _height:CGFloat! = self.navigationController?.navigationBar.layer.frame.height
            var navBarBg = UIView(frame:CGRectMake(0, 0, _width, _height))
            //solid color for bg
            navBarBg.backgroundColor = UIColor.orangeColor()
            view.addSubview(navBarBg)

两条线的解决方案对我来说很有效。尝试在ViewDidLoad方法中添加这个:

navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
self.extendedLayoutIncludesOpaqueBars = true

你应该在UISearchBar的底部添加一个视图

let rect = searchController.searchBar.frame;
let lineView : UIView = UIView.init(frame: CGRect.init(x: 0, y: rect.size.height-1, width: rect.size.width, height: 1))
lineView.backgroundColor = UIColor.init(hexString: "8CC73E")
searchController.searchBar.addSubview(lineView)