如何隐藏UINavigationBar 1px底线

时间:2013-10-07 14:04:48

标签: ios objective-c swift ipad uinavigationbar

我有一个应用程序有时需要其导航栏与内容融合。

有谁知道如何摆脱或改变这个讨厌的小酒吧的颜色?

在下图中,我有 - 我正在谈论“根视图控制器”下面的这条1px高度线

enter image description here

49 个答案:

答案 0 :(得分:643)

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

  

要显示自定义阴影图像,必须使用自定义背景图像   也可以使用setBackgroundImage(_:for :)方法设置。如果是默认值   使用背景图像,然后将使用默认阴影图像   无论此属性的值如何。

所以:

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

以上是隐藏它的唯一“官方”方式。不幸的是,它消除了酒吧的半透明度。

我不想要背景图片,只需要颜色

你有这些选择:

  1. 纯色,无透明度:

    navigationBar.barTintColor = UIColor.redColor()
    navigationBar.isTranslucent = false
    navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationBar.shadowImage = UIImage()
    
  2. 创建填充颜色的小背景图像并使用它。

  3. 使用下面描述的'hacky'方法。它也会保持半透明状态。

  4. 如何保持酒吧半透明?

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

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

    1. 声明实例变量:

      private var shadowImageView: UIImageView?
      
    2. 添加找到此阴影(发际线)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. 添加/修改viewWillAppear/viewWillDisappear方法:

      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
      }
      
    4. 同样的方法也适用于UISearchBar发际线, (几乎)你需要隐藏的任何东西:)

      非常感谢@Leo Natan最初的想法!

答案 1 :(得分:195)

以下简单可以帮助您!

<强>夫特:

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

目标C:

[self.navigationController.navigationBar setValue:@(YES) forKeyPath:@"hidesShadow"];

答案 2 :(得分:142)

如果您只想使用实心导航栏颜色并在故事板中进行设置,请在AppDelegate课程中使用此代码通过外观代理删除1像素边框:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
                                  forBarPosition:UIBarPositionAny
                                      barMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

答案 3 :(得分:94)

试试这个:

[[UINavigationBar appearance] setBackgroundImage: [UIImage new]  
                                   forBarMetrics: UIBarMetricsDefault];

[UINavigationBar appearance].shadowImage = [UIImage new];

下面的图片有解释(iOS7 NavigationBar):

enter image description here

并检查这个问题: iOS7 - Change UINavigationBar border color

答案 4 :(得分:60)

想要添加Swift版本的Serhii的答案。我使用以下内容创建了UIBarExtension.swift

import Foundation
import UIKit

extension UINavigationBar {
    func hideBottomHairline() {
        self.hairlineImageView?.isHidden = true
    }

    func showBottomHairline() {
        self.hairlineImageView?.isHidden = false
    }
}

extension UIToolbar {
    func hideBottomHairline() {
        self.hairlineImageView?.isHidden = true
    }

    func showBottomHairline() {
        self.hairlineImageView?.isHidden = false
    }
}

extension UIView {
    fileprivate var hairlineImageView: UIImageView? {
        return hairlineImageView(in: self)
    }

    fileprivate func hairlineImageView(in view: UIView) -> UIImageView? {
        if let imageView = view as? UIImageView, imageView.bounds.height <= 1.0 {
            return imageView
        }

        for subview in view.subviews {
            if let imageView = self.hairlineImageView(in: subview) { return imageView }
        }

        return nil
    }
}

答案 5 :(得分:58)

快速做到这一点:

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

UINavigationBar.appearance().shadowImage = UIImage()

答案 6 :(得分:18)

swift中的简单解决方案

   let navigationBar = self.navigationController?.navigationBar
    navigationBar?.setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
    navigationBar?.shadowImage = UIImage()

答案 7 :(得分:16)

在研究了Serhil的答案后,我创建了一个可以轻松隐藏发际线的吊舱UINavigationBar+Addition

#import "UINavigationBar+Addition.h"

- (void)viewDidLoad {
    [super viewDidLoad];

    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    [navigationBar hideBottomHairline];
}

答案 8 :(得分:13)

在Swift 3.0中

通过将以下代码添加到您的应用程序功能来编辑您的AppDelegate.swift

// Override point for customization after application launch.

// Remove border in navigationBar
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)

答案 9 :(得分:11)

pxpgraphics' solution针对Swift 2.0进行了更新

extension UINavigationBar {

    func hideBottomHairline()
    {
        hairlineImageViewInNavigationBar(self)?.hidden = true
    }

    func showBottomHairline()
    {
        hairlineImageViewInNavigationBar(self)?.hidden = false
    }

    private func hairlineImageViewInNavigationBar(view: UIView) -> UIImageView?
    {
        if let imageView = view as? UIImageView where imageView.bounds.height <= 1
        {
            return imageView
        }

        for subview: UIView in view.subviews
        {
            if let imageView = hairlineImageViewInNavigationBar(subview)
            {
                return imageView
            }
        }

        return nil
    }

}

extension UIToolbar
{

    func hideHairline()
    {
        let navigationBarImageView = hairlineImageViewInToolbar(self)?.hidden = true
    }

    func showHairline()
    {
        let navigationBarImageView = hairlineImageViewInToolbar(self)?.hidden = false
    }

    private func hairlineImageViewInToolbar(view: UIView) -> UIImageView?
    {
        if let imageView = view as? UIImageView where imageView.bounds.height <= 1
        {
            return imageView
        }

        for subview: UIView in view.subviews
        {
            if let imageView = hairlineImageViewInToolbar(subview)
            {
                return imageView
            }
        }

        return nil
    }

}

答案 10 :(得分:9)

我使用UINavigationBar扩展,使我能够使用UIAppearance API隐藏/显示阴影,或者使用Storyboard(或源代码)选择隐藏/显示阴影的导航栏。这是扩展名:

import UIKit

private var flatAssociatedObjectKey: UInt8 = 0

/*
  An extension that adds a "flat" field to UINavigationBar. This flag, when
  enabled, removes the shadow under the navigation bar.
 */
@IBDesignable extension UINavigationBar {
    @IBInspectable var flat: Bool {
        get {
            guard let obj = objc_getAssociatedObject(self, &flatAssociatedObjectKey) as? NSNumber else {
                return false
            }
            return obj.boolValue;
        }

        set {
            if (newValue) {
                let void = UIImage()
                setBackgroundImage(void, forBarPosition: .Any, barMetrics: .Default)
                shadowImage = void
            } else {
                setBackgroundImage(nil, forBarPosition: .Any, barMetrics: .Default)
                shadowImage = nil
            }
            objc_setAssociatedObject(self, &flatAssociatedObjectKey, NSNumber(bool: newValue),
                    objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}

现在,要禁用所有导航栏上的阴影,您必须使用:

UINavigationBar.appearance().flat = true

或者您可以使用故事板启用/禁用此行为:

Navigation Bar Storyboard

答案 11 :(得分:9)

Swift 4 //用于隐藏导航栏阴影线

navigationController?.navigationBar.shadowImage = UIImage()

答案 12 :(得分:8)

Swift 4已测试 一线解决方案

Viewdidload()中 将键“ hidesShadow”的导航控制器的用户默认值设置为true

override func viewDidLoad() {
    super.viewDidLoad()

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

}

答案 13 :(得分:7)

如果您想要保留半透明度并且不想在应用中为每个UINavigationController创建子类,则另一个选项是:

#import <objc/runtime.h>

@implementation UINavigationController (NoShadow)

+ (void)load {
    Method original = class_getInstanceMethod(self, @selector(viewWillAppear:));
    Method swizzled = class_getInstanceMethod(self, @selector(swizzled_viewWillAppear:));
    method_exchangeImplementations(original, swizzled);
}

+ (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }

    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }

    return nil;
}

- (void)swizzled_viewWillAppear:(BOOL)animated {
    UIImageView *shadow = [UINavigationController findHairlineImageViewUnder:self.navigationBar];
    shadow.hidden = YES;

    [self swizzled_viewWillAppear:animated];
}

@end

答案 14 :(得分:6)

斯威夫特放了这个

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

in

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

答案 15 :(得分:5)

Slightly Swift Solution 
func setGlobalAppearanceCharacteristics () {
    let navigationBarAppearace = UINavigationBar.appearance()
    navigationBarAppearace.tintColor = UIColor.white
    navigationBarAppearace.barTintColor = UIColor.blue
    navigationBarAppearace.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    navigationBarAppearace.shadowImage = UIImage()

}

答案 16 :(得分:4)

在iOS8中,如果将UINavigationBar.barStyle设置为.Black,则可以将条形图的背景设置为没有边框的纯色。

在斯威夫特:

UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().barStyle = UIBarStyle.Black
UINavigationBar.appearance().barTintColor = UIColor.redColor()

答案 17 :(得分:4)

也可以从情节提要中隐藏(在Xcode 10.1上使用)

通过添加运行时属性: hidesShadow-Boolean-True

enter image description here

答案 18 :(得分:3)

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

self.navigationController.navigationBar.clipsToBounds = YES;

答案 19 :(得分:3)

Swift 4.2中的解决方案:

private func removeHairlineFromNavbar() {
    UINavigationBar.appearance().setBackgroundImage(
        UIImage(),
        for: .any,
        barMetrics: .default)
    UINavigationBar.appearance().shadowImage = UIImage()
}

只需将此函数放在第一个Viewcontroller中,然后在viewdidload中调用

答案 20 :(得分:2)

您应该将视图添加到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)

答案 21 :(得分:2)

从iOS 13开始,有一个用于设置或删除阴影的系统API

  

UIKit使用shadowImage和shadowColor属性确定阴影的   出现。当shadowImage为nil时,条形图将显示默认阴影   根据shadowColor属性中的值。如果shadowColor为nil或   包含clearColor颜色,该条不显示阴影。

    let appearance = UINavigationBarAppearance()
    appearance.shadowImage = nil
    appearance.shadowColor = nil
    navigationController.navigationBar.standardAppearance = appearance

答案 22 :(得分:2)

设置背景图片的问题是它会消除模糊。您可以在不设置背景图像的情况下将其删除。请参阅我的回答here

答案 23 :(得分:1)

我知道这是一个老线程,但我找到了一个非常有效的解决方案:

子类UINavigationBar。 在您的UINavigationBar子类中,使用以下代码覆盖didAddSubview:

- (void)didAddSubview:(UIView *)subview
{
    [super didAddSubview:subview];

    if ([subview isKindOfClass:[UIImageView class]]) {
        [subview setClipsToBounds:YES];
    }
}

答案 24 :(得分:1)

Swift 3 中我们这样做

对于任何视图控制器:

Empty list doesn't contain element at index 1.

对于整个应用:

navigationBar.shadowImage = UIImage()
setBackgroundImage(UIImage(), for: .default)

答案 25 :(得分:1)

这是一种无需使用任何图像即可完成此操作的方法,这是对我有用的唯一方法:

self.navigationController.navigationBar.layer.shadowOpacity = 0;

不幸的是,您需要在每个不希望显示该行的文件上执行此操作。在appDelegate中无法这样做。

编辑:

不需要将shadowColor设置为nil,这是您唯一需要的行。

答案 26 :(得分:1)

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
  }
}

答案 27 :(得分:1)

目标C回答上述问题

//删除1px导航栏

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setTranslucent:NO];
[[UINavigationBar appearance] setTintColor:[UIColor yourColor]];

答案 28 :(得分:1)

对于iOS 9用户,这对我有用。只需添加:

UINavigationBar.appearance().shadowImage = UIImage()

答案 29 :(得分:1)

AppDelegate 中,这全局更改了导航栏的格式:

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

    UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
    UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().barTintColor = UIColor.redColor()
    UINavigationBar.appearance().translucent = false
    UINavigationBar.appearance().clipsToBounds = false
    UINavigationBar.appearance().backgroundColor = UIColor.redColor()
    UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : (UIFont(name: "FONT NAME", size: 18))!, NSForegroundColorAttributeName: UIColor.whiteColor()] }

没有设法在特定的VC上实现任何不同的东西,但这将有助于90%的人

答案 30 :(得分:1)

EVALUATE TRUE
    WHEN COD-USER = 01
        DO-SOMETHING
    WHEN COD-USER = 02
        DO-SOMETHING-ELSE
    WHEN OTHER
        ADD 1 TO CTN-ERROR
END-EVALUATE

答案 31 :(得分:1)

我刚刚为此创建了一个扩展程序...抱歉格式化(这是我的第一个答案)。

用法:

  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
    }


}

答案 32 :(得分:0)

简单的解决方案– Swift 5

  1. 创建扩展程序:

    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
    }}
    
  2. 将此添加到viewDidLoad()

    self.navigationController?.navigationBar.shadowImage = UIImage.hideNavBarLine(color: UIColor.clear)
    

答案 33 :(得分:0)

适用于我的两行解决方案。尝试在ViewDidLoad方法中添加它:

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

答案 34 :(得分:0)

这里有一个非常重要的注意事项 - 更改 UIViewControllernavigationItem 的外观比直接更改 navigationBar 的外观要灵活得多。

为什么这么问?

原因很简单,navigationItem 与单个 UIViewController 相关联并代表该特定 navigationBarUIViewController 状态。这很重要,因为您不必处理 viewWillAppear(或类似的东西)内不同视图控制器之间的导航栏变化,就像您改变 navigationBar;请记住,它在给定导航堆栈 (UINavigationController) 的所有视图控制器之间共享,并且在一个地方更改它会更改直到堆栈的所有视图控制器。

您只需为特定视图控制器设置正确的 UINavigationBarAppearance,UIKit 将根据当前导航堆栈中的顶部视图控制器的视图控制器正确更新导航栏样式。

navigationItem.standardAppearance` = `UINavigationBarAppearance()

答案 35 :(得分:0)

        if #available(iOS 13.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor          = Colors.color_app
            appearance.titleTextAttributes      = [.foregroundColor : UIColor.white]
            appearance.largeTitleTextAttributes = [.foregroundColor : UIColor.white]
            appearance.shadowColor = .clear
            appearance.shadowImage = UIImage()
            
            UINavigationBar.appearance().tintColor            = .white
            UINavigationBar.appearance().standardAppearance   = appearance
            UINavigationBar.appearance().compactAppearance    = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
        } else {
            UINavigationBar.appearance().barTintColor        = Colors.color_app
            UINavigationBar.appearance().tintColor           = .white
            UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
            if #available(iOS 11.0, *) {
                UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
            }
            UINavigationBar.appearance().isTranslucent = false
            
            UINavigationBar.appearance().shadowImage = UIImage()
            UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
        }

答案 36 :(得分:0)

编写您自己的初始化程序:D

import Foundation
import UIKit

extension UINavigationController {
    convenience init(rootViewController : UIViewController, hidesShadow : Bool) {
        self.init(rootViewController : rootViewController)
        self.navigationBar.setValue(hidesShadow, forKey: "hidesShadow")
        if hidesShadow {
            self.extendedLayoutIncludesOpaqueBars = true
            self.navigationBar.isTranslucent = false 
        }
    }
}

答案 37 :(得分:0)

不使用navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")非常重要,因为Apple随时可以删除“ hidesShadow”键路径。如果他们这样做,则使用此调用的任何应用程序都会中断。由于您没有访问类的直接API,因此此调用会遭到App Store拒绝。

从iOS 13开始,要确保效率,您可以执行以下操作:

navigationBar.standardAppearance.shadowColor = nil

答案 38 :(得分:0)

嗨,这适用于Swift 4。

    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.isTranslucent = false
}

你需要把它放在viewDidLayoutSubviews而不是viewDidLoad

答案 39 :(得分:0)

Xamarin表格中,这对我有用。只需在AppDelegate.cs上添加:

UINavigationBar.Appearance.ShadowImage = new UIImage();

答案 40 :(得分:0)

我遇到了同样的问题,没有一个答案真的让人满意。这是我对Swift3的看法:

func hideNavigationBarLine() {
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
}

只需在 viewDidLoad()中调用此方法。

答案 41 :(得分:0)

在子视图中找到细线的一个很好的短Swift函数就是这个:

 func findHairLineInImageViewUnder(view view: UIView) -> UIImageView? {
    if let hairLineView = view as? UIImageView where hairLineView.bounds.size.height <= 1.0 {
        return hairLineView
    }

    if let hairLineView = view.subviews.flatMap({self.findHairLineInImageViewUnder(view: $0)}).first {
      return hairLineView
    }

    return nil
}

答案 42 :(得分:0)

对我来说最简单的是,用所需的颜色创建一个png(它只需要一个像素一维的像素),然后将backgroundImage和shadowImage设置为:

let greenPixel = UIImage(named: "TheNameOfYourPng")
navigationBar.setBackgroundImage(greenPixel, forBarMetrics: UIBarMetrics.Default)
navigationBar.shadowImage = greenPixel

答案 43 :(得分:0)

酒吧风格黑色为我做了。

[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];

我拥有的所有属性(以防万一):

    [[UINavigationBar appearance] setBarTintColor:color];
    [[UINavigationBar appearance] setTranslucent:NO];
    [[UINavigationBar appearance] setShadowImage:[UIImage new]];
    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];

答案 44 :(得分:0)

这是另一种选择 - 我认为只有在你的导航栏上不需要半透明时才会有效(我没有)。我刚刚在导航栏底部(导航栏下方1个像素)添加了一个1像素高的UIView,颜色与我的导航栏相同:

UIView *view = [[UIView alloc] init];
[view setBackgroundColor:self.navigationController.navigationBar.barTintColor];
[self.navigationController.navigationBar addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.height.equalTo(@(1.0f));
    make.leading.trailing.equalTo(self.navigationController.navigationBar);
    make.bottom.equalTo(self.navigationController.navigationBar).offset(1.0f);
}];

我使用Masonry添加约束。

答案 45 :(得分:0)

我的方法:

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)

答案 46 :(得分:0)

[tabviewController.view setBackgroundColor:[UIColor blackColor]];

对我而言[UIColor blackColor]可能是你的背景色, 如果您正在使用它,则tabviewController是您的UITabBarController

答案 47 :(得分:0)

这可能听起来很愚蠢,但只有当viewController视图的背景颜色设置为任何颜色但白色时,才会出现此发际线。我很震惊地了解到这一事实。

因此,如果你想让它消失而没有太多麻烦,只需将控制器的视图背景颜色设置为白色。

答案 48 :(得分:-3)

您也可以使用

self.navigationController?.hidesNavigationBarHairline = true

作为Swift的新增功能,这是一种完成与上述解决方案相同的事情的简单方法

编辑:经过一些测试,显然这仅适用于ChameleonFramework可可豆荚!