如何隐藏UINavigationBar 1px底线 [英] How to hide UINavigationBar 1px bottom line

查看:90
本文介绍了如何隐藏UINavigationBar 1px底线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

On the image below situation i have - i'm talking about this 1px height line below "Root View Controller"

推荐答案

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

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:


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

For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage(_:for:) method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

所以:

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

以上是隐藏它的唯一官方方式。不幸的是,它删除了条形图的半透明度。

您有以下选择:


  1. 纯色,无半透明:

  1. Solid color, no translucency:

navigationBar.barTintColor = UIColor.redColor()
navigationBar.isTranslucent = false
navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.shadowImage = UIImage()


  • 创建填充颜色的小背景图像并使用它。

  • Create small background image filled with color and use it.

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

    Use 'hacky' method described below. It will also keep bar translucent.



    如何保持酒吧半透明?



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

    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. We can find it and hide/show it when needed.

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

    Instructions below assume you need hairline hidden only in one controller of your UINavigationController hierarchy.


    1. 声明实例变量:

    1. Declare instance variable:

    private var shadowImageView: 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
    }
    


  • 添加/修改 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
    }
    


  • 同样的方法也适用于 UISearchBar 发际线,
    和(差不多)你需要隐藏的其他东西:)

    The same method should also work for UISearchBar hairline, and (almost) anything else you need to hide :)

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

    这篇关于如何隐藏UINavigationBar 1px底线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆