ios 如何隐藏 UINavigationBar 1px 底线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19226965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:10:15  来源:igfitidea点击:

How to hide UINavigationBar 1px bottom line

iosobjective-cswiftipaduinavigationbar

提问by Szymon Kuczur

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 高度线

enter image description here

enter image description here

回答by Serhii Yakovenko

For iOS 13:

对于 iOS 13:

Use the .shadowColorproperty

使用.shadowColor物业

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

如果此属性为零或包含清晰颜色,则条形图不显示阴影

For instance:

例如:

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

For iOS 12 and below:

对于 iOS 12 及更低版本:

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. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

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

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. It will also keep bar translucent.

  1. 纯色,无半透明:

    navigationBar.barTintColor = UIColor.redColor()
    navigationBar.isTranslucent = false
    navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationBar.shadowImage = UIImage()
    
  2. 创建填充颜色的小背景图像并使用它。

  3. 使用下面描述的“hacky”方法。它也将保持酒吧半透明。

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

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

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

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

  1. Declare instance variable:

    private var shadowImageView: UIImageView?
    
  2. Add method which finds this shadow (hairline) 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/viewWillDisappearmethods:

    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
    }
    
  1. 声明实例变量:

    private var shadowImageView: UIImageView?
    
  2. 添加找到此阴影(细线)的方法 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. 添加/编辑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 UISearchBarhairline, and (almost) anything else you need to hide :)

相同的方法也适用于UISearchBar发际线,以及(几乎)您需要隐藏的任何其他内容:)

Many thanks to @Leo Natan for the original idea!

非常感谢@Leo Natan 的原创想法!

回答by Vishnuvardhan

Below can help simply!

下面可以简单地帮助!

Swift:

迅速:

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

Objective C:

目标 C:

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

回答by Rick Pastoor

If you just want to use a solid navigation bar color and have set this up in your storyboard, use this code in your AppDelegateclass 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]];

回答by Tarek Hallak

Try this:

尝试这个:

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

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

Below image has the explanation (iOS7 NavigationBar):

下图有说明(iOS7 NavigationBar):

enter image description here

enter image description here

And check this SO question: iOS7 - Change UINavigationBar border color

并检查这个问题: iOS7 - Change UINavigationBar border color

回答by pxpgraphics

Wanted to add the Swift version of Serhii's answer. I created a UIBarExtension.swiftwith the following:

想要添加 Serhii 答案的 Swift 版本。我创建了UIBarExtension.swift以下内容:

import Foundation
import UIKit

extension UINavigationBar {
    func hideBottomHairline() {
        self.hairlineImageView?.isHidden = true
    }

    func showBottomHairline() {
        self.hairlineImageView?.isHidden = false
    }
}

extension UIToolbar {
    func hideBottomHairline() {
        self.hairlineImageView?.isHidden = true
    }

    func showBottomHairline() {
        self.hairlineImageView?.isHidden = false
    }
}

extension UIView {
    fileprivate var hairlineImageView: UIImageView? {
        return hairlineImageView(in: self)
    }

    fileprivate func hairlineImageView(in view: UIView) -> UIImageView? {
        if let imageView = view as? UIImageView, imageView.bounds.height <= 1.0 {
            return imageView
        }

        for subview in view.subviews {
            if let imageView = self.hairlineImageView(in: subview) { return imageView }
        }

        return nil
    }
}

回答by OscarVGG

The swift way to do it:

快速的方法:

UINavigationBar.appearance().setBackgroundImage(
    UIImage(),
    forBarPosition: .Any,
    barMetrics: .Default)

UINavigationBar.appearance().shadowImage = UIImage()

回答by Jakub Pr??a

Simple solution in swift

快速的简单解决方案

   let navigationBar = self.navigationController?.navigationBar
    navigationBar?.setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
    navigationBar?.shadowImage = UIImage()

回答by smohn

In Swift 3.0

在 Swift 3.0 中

Edit your AppDelegate.swiftby adding the following code to your application function:

AppDelegate.swift通过将以下代码添加到您的应用程序功能来编辑您的:

// Override point for customization after application launch.

// Remove border in navigationBar
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)

回答by samwize

After studying the answer from Serhil, I created a pod UINavigationBar+Additionthat can easily hide the hairline.

在研究了Serhil的回答后,我创建了一个可以轻松隐藏发际线的pod UINavigationBar+Addition

#import "UINavigationBar+Addition.h"

- (void)viewDidLoad {
    [super viewDidLoad];

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

回答by glotcha

As of iOS 13 there is a system API to set or remove the shadow

从 iOS 13 开始,有一个系统 API 来设置或删除阴影

UIKit uses shadowImage and the shadowColor property to determine the shadow's appearance. When shadowImage is nil, the bar displays a default shadow tinted according to the value in the shadowColor property. If shadowColor is nil or contains the clearColor color, the bar displays no shadow.

UIKit 使用 shadowImage 和 shadowColor 属性来确定阴影的外观。当 shadowImage 为 nil 时,栏显示默认阴影,根据 shadowColor 属性中的值着色。如果 shadowColor 为 nil 或包含 clearColor 颜色,则条形图不显示阴影。

    let appearance = UINavigationBarAppearance()
    appearance.shadowImage = nil
    appearance.shadowColor = nil
    navigationController.navigationBar.standardAppearance = appearance

https://developer.apple.com/documentation/uikit/uibarappearance/3198009-shadowimage

https://developer.apple.com/documentation/uikit/uibarappearance/3198009-shadowimage