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

查看:28
本文介绍了如何隐藏 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"

推荐答案

对于 iOS 13:

使用 .shadowColor 属性

如果该属性为 nil 或包含透明色,则条形图不显示阴影

If this property is nil or contains the clear color, the bar displays no shadow

例如:

let navigationBar = navigationController?.navigationBar
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.shadowColor = .clear
navigationBar?.scrollEdgeAppearance = navigationBarAppearance

对于 iOS 12 及更低版本:

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

For iOS 12 and below:

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天全站免登陆