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

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

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


当前回答

下面是破解方法。因为它在关键路径上工作,将来可能会崩溃。但就目前而言,它的运作符合预期。

迅速:

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

Objective - C:

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

其他回答

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

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

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

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

    if ([subview isKindOfClass:[UIImageView class]]) {
        [subview setClipsToBounds:YES];
    }
}
[tabviewController.view setBackgroundColor:[UIColor blackColor]];

对我来说[UIColor blackColor]可能是你的背景色, tabviewController是你的UITabBarController,如果你正在使用它!

下面是破解方法。因为它在关键路径上工作,将来可能会崩溃。但就目前而言,它的运作符合预期。

迅速:

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

Objective - C:

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

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