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

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

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


当前回答

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()

}

其他回答

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

子类UINavigationBar。 在你的UINavigationBar子类中,用下面的代码重写didAddSubview:

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

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

在子视图中找到发际线的一个很好的简短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
}

简单的解决方案- 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)

在研究了Serhil的答案后,我创建了一个豆荚UINavigationBar+Addition,可以很容易地隐藏发际线。

#import "UINavigationBar+Addition.h"

- (void)viewDidLoad {
    [super viewDidLoad];

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

试试这个:

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

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

下图有解释(iOS7导航栏):

检查这个SO问题: iOS7 -改变UINavigationBar的边框颜色