如何隱藏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];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章