如何隐藏UINavigationBar 1px底线

本文翻译自:How to hide UINavigationBar 1px bottom line

I have an app that sometimes needs its navigation bar to blend in with the content. 我有一个应用程序,有时需要其导航栏与内容融为一体。

Does anyone know how to get rid of or to change color of this annoying little bar? 有谁知道如何摆脱或改变这个讨厌的小酒吧的颜色?

On the image below situation i have - i'm talking about this 1px height line below "Root View Controller" 在下面的图像我有 - 我正在谈论“根视图控制器”下面这个1px高度线

在此输入图像描述


#1楼

参考:https://stackoom.com/question/1IfoL/如何隐藏UINavigationBar-px底线


#2楼

Try this: 试试这个:

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

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

Below image has the explanation (iOS7 NavigationBar): 下面的图片有解释(iOS7 NavigationBar):

在此输入图像描述

And check this SO question: iOS7 - Change UINavigationBar border color 并检查这个问题: iOS7 - 更改UINavigationBar边框颜色


#3楼

To do this, you should set a custom shadow image. 为此,您应该设置自定义阴影图像。 But for the shadow image to be shown you also need to set a custom background image, quote from Apple's documentation: 但是要显示阴影图像,您还需要设置自定义背景图像,引用Apple的文档:

For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage(_:for:) method. 要显示自定义阴影图像,还必须使用setBackgroundImage(_:for :)方法设置自定义背景图像。 If the default background image is used, then the default shadow image will be used regardless of the value of this property. 如果使用默认背景图像,则无论此属性的值如何,都将使用默认阴影图像。

So: 所以:

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

Above is the only "official" way to hide it. 以上是隐藏它的唯一“官方”方式。 Unfortunately, it removes bar's translucency. 不幸的是,它消除了酒吧的半透明度。

I don't want background image, just color 我不想要背景图片,只需要颜色

You have those options: 你有这些选择:

  1. Solid color, no translucency: 纯色,无透明度:

     navigationBar.barTintColor = UIColor.redColor() navigationBar.isTranslucent = false navigationBar.setBackgroundImage(UIImage(), for: .default) navigationBar.shadowImage = UIImage() 
  2. Create small background image filled with color and use it. 创建填充颜色的小背景图像并使用它。

  3. Use 'hacky' method described below. 使用下面描述的'hacky'方法。 It will also keep bar translucent. 它还会保持酒吧半透明。

How to keep bar translucent? 如何保持酒吧半透明?

To keep translucency you need another approach, it looks like a hack but works well. 为了保持半透明,你需要另一种方法,它看起来像一个黑客,但运作良好。 The shadow we're trying to remove is a hairline UIImageView somewhere under UINavigationBar . 我们试图删除的阴影是在UINavigationBar下的某个地方的细线UIImageView We can find it and hide/show it when needed. 我们可以找到它并在需要时隐藏/显示它。

Instructions below assume you need hairline hidden only in one controller of your UINavigationController hierarchy. 下面的说明假设您只需要在UINavigationController层次结构的一个控制器中隐藏发际线。

  1. Declare instance variable: 声明实例变量:

     private var shadowImageView: UIImageView? 
  2. Add method which finds this shadow (hairline) UIImageView: 找到这个阴影(发际线) UIImageView:添加方法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. Add/edit viewWillAppear/viewWillDisappear methods: 添加/编辑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 } 

The same method should also work for UISearchBar hairline, and (almost) anything else you need to hide :) 同样的方法也适用于UISearchBar发际线,并且(几乎)你需要隐藏的任何其他东西:)

Many thanks to @Leo Natan for the original idea! 非常感谢@Leo Natan最初的想法!


#4楼

If you just want to use a solid navigation bar color and have set this up in your storyboard, use this code in your AppDelegate class to remove the 1 pixel border via the appearance proxy: 如果您只想使用实心导航栏颜色并在故事板中进行设置,请在AppDelegate类中使用此代码通过外观代理删除1像素边框:

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

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

#5楼

The problem with setting a background image is it removes blurring. 设置背景图像的问题是它会消除模糊。 You can remove it without setting a background image. 您可以在不设置背景图像的情况下将其删除。 See my answer here . 在这里看到我的答案。


#6楼

After studying the answer from Serhil, I created a pod UINavigationBar+Addition that can easily hide the hairline. 在研究了Serhil的答案后,我创建了一个可以轻松隐藏发际线的吊舱UINavigationBar + Addition

#import "UINavigationBar+Addition.h"

- (void)viewDidLoad {
    [super viewDidLoad];

    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    [navigationBar hideBottomHairline];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值