【问题标题】:How to hide UINavigationBar 1px bottom line如何隐藏 UINavigationBar 1px 底线
【发布时间】:2013-10-14 03:38:49
【问题描述】:

我有一个应用有时需要其导航栏才能与内容融为一体。

有谁知道如何摆脱这个烦人的小酒吧或改变它的颜色?

在下图的情况下,我说的是“根视图控制器”下方的 1px 高度线

【问题讨论】:

标签: ios objective-c swift ipad uinavigationbar


【解决方案1】:

你应该在 UISearchBar 的底部添加一个视图

let rect = searchController.searchBar.frame;
let lineView : UIView = UIView.init(frame: CGRect.init(x: 0, y: rect.size.height-1, width: rect.size.width, height: 1))
lineView.backgroundColor = UIColor.init(hexString: "8CC73E")
searchController.searchBar.addSubview(lineView)

【讨论】:

  • 我不敢相信这仍然是一个问题 - 又遇到了!但是这个解决方案是目前最好的解决方案。谢谢@Socheat
【解决方案2】:

在 Swift 3.0 中

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

// Override point for customization after application launch.

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

【讨论】:

    【解决方案3】:

    Swift 3 中我们这样做

    对于任何视图控制器:

    navigationBar.shadowImage = UIImage()
    setBackgroundImage(UIImage(), for: .default)
    

    对于整个应用:

    UINavigationBar.appearance().setBackgroundImage(UIImage(),barMetrics: .Default)
    UINavigationBar.appearance().shadowImage = UIImage()
    

    【讨论】:

    • 需要 Swift 4:UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default) UINavigationBar.appearance().shadowImage = UIImage()
    • 必须在didFinishLaunchingWithOptions的AppDelegate中调用
    【解决方案4】:

    我遇到了同样的问题,没有一个答案是真正令人满意的。这是我对 Swift3 的看法:

    func hideNavigationBarLine() {
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
    }
    

    只需在 viewDidLoad() 中调用它。

    【讨论】:

      【解决方案5】:

      Xamarin 表单 中,这对我有用。只需将其添加到 AppDelegate.cs:

      UINavigationBar.Appearance.ShadowImage = new UIImage();
      

      【讨论】:

        【解决方案6】:

        斯威夫特 4 //用于隐藏导航栏阴影线

        navigationController?.navigationBar.shadowImage = UIImage()
        

        【讨论】:

          【解决方案7】:

          您好,这适用于 Swift 4。

              override func viewDidLayoutSubviews() {
              super.viewDidLayoutSubviews()
              self.navigationController?.navigationBar.shadowImage = UIImage()
              self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
              self.navigationController?.navigationBar.isTranslucent = false
          }
          

          你需要把它放在 viewDidLayoutSubviews 而不是 viewDidLoad

          【讨论】:

            【解决方案8】:

            这是一种不使用任何图像的方法,这是唯一对我有用的方法:

            self.navigationController.navigationBar.layer.shadowOpacity = 0;
            

            不幸的是,您需要在希望该行不出现的每个文件上执行此操作。在appDelegate 中没有办法这样做。

            编辑:

            不需要将shadowColor 设置为nil,这是您唯一需要的行。

            【讨论】:

              【解决方案9】:

              Swift 4 测试 单线解决方案

              Viewdidload() 为键“hidesShadow”设置导航控制器的用户默认值 true

              override func viewDidLoad() {
                  super.viewDidLoad()
              
                  self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
              
              }
              

              【讨论】:

              • 完美运行!!
              【解决方案10】:

              也可以从 Storyboard 中隐藏(在 Xcode 10.1 上工作)

              通过添加运行时属性:hidesShadow - Boolean - True

              【讨论】:

                【解决方案11】:

                Swift 4.2 中的解决方案:

                private func removeHairlineFromNavbar() {
                    UINavigationBar.appearance().setBackgroundImage(
                        UIImage(),
                        for: .any,
                        barMetrics: .default)
                    UINavigationBar.appearance().shadowImage = UIImage()
                }
                

                只要把这个函数放在第一个Viewcontroller中,在viewdidload中调用

                【讨论】:

                  【解决方案12】:

                  从 iOS 13 开始,有一个系统 API 可以设置或移除阴影

                  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

                  【讨论】:

                  • shadowImage 和 shadowColor 没有记录在 UINavigationBarAppearance 标题中!!我永远找不到它。谢谢,这解决了我的问题 100%
                  • 是的,它在 iOS 13 中对我来说也可以正常工作,非常感谢@glotcha。
                  【解决方案13】:

                  简单的解决方案——Swift 5

                  1. 创建一个扩展:

                    extension UIImage {
                    
                        class func hideNavBarLine(color: UIColor) -> UIImage? {
                    
                            let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
                            UIGraphicsBeginImageContext(rect.size)
                            let context = UIGraphicsGetCurrentContext()
                            context?.setFillColor(color.cgColor)
                            context?.fill(rect)
                    
                    
                            let navBarLine = UIGraphicsGetImageFromCurrentImageContext()
                            UIGraphicsEndImageContext()
                            return navBarLine
                        }
                    }
                    
                  2. 将此添加到viewDidLoad()

                    self.navigationController?.navigationBar.shadowImage = UIImage.hideNavBarLine(color: UIColor.clear)
                    

                  【讨论】:

                    【解决方案14】:

                    适合我的两行解决方案。尝试在 ViewDidLoad 方法中添加这个:

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

                    【讨论】:

                      【解决方案15】:
                              if #available(iOS 13.0, *) {
                                  let appearance = UINavigationBarAppearance()
                                  appearance.backgroundColor          = Colors.color_app
                                  appearance.titleTextAttributes      = [.foregroundColor : UIColor.white]
                                  appearance.largeTitleTextAttributes = [.foregroundColor : UIColor.white]
                                  appearance.shadowColor = .clear
                                  appearance.shadowImage = UIImage()
                                  
                                  UINavigationBar.appearance().tintColor            = .white
                                  UINavigationBar.appearance().standardAppearance   = appearance
                                  UINavigationBar.appearance().compactAppearance    = appearance
                                  UINavigationBar.appearance().scrollEdgeAppearance = appearance
                              } else {
                                  UINavigationBar.appearance().barTintColor        = Colors.color_app
                                  UINavigationBar.appearance().tintColor           = .white
                                  UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
                                  if #available(iOS 11.0, *) {
                                      UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
                                  }
                                  UINavigationBar.appearance().isTranslucent = false
                                  
                                  UINavigationBar.appearance().shadowImage = UIImage()
                                  UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
                              }
                      

                      【讨论】:

                        【解决方案16】:

                        编写你自己的初始化器:D

                        import Foundation
                        import UIKit
                        
                        extension UINavigationController {
                            convenience init(rootViewController : UIViewController, hidesShadow : Bool) {
                                self.init(rootViewController : rootViewController)
                                self.navigationBar.setValue(hidesShadow, forKey: "hidesShadow")
                                if hidesShadow {
                                    self.extendedLayoutIncludesOpaqueBars = true
                                    self.navigationBar.isTranslucent = false 
                                }
                            }
                        }
                        

                        【讨论】:

                          【解决方案17】:

                          不要使用navigationController?.navigationBar.setValue(true, forKey: "hidesShadow") 非常重要,因为在任何时候,Apple 都可以删除“hidesShadow”键路径。如果他们这样做,任何使用此调用的应用程序都会中断。由于您没有访问类的直接 API,因此此调用可能会遭到 App Store 拒绝。

                          从 iOS 13 开始,为确保效率,您可以执行以下操作:

                          navigationBar.standardAppearance.shadowColor = nil

                          【讨论】:

                          • 您的回答将破坏isTranslucent = false 的设置,背景颜色将被更改。到目前为止,只有“不使用”的答案对我有用,即使在最新的 iOS 14.4 中也是如此
                          【解决方案18】:

                          这里有一个非常重要的注意事项 - 与直接更改 navigationBar 相比,更改 UIViewControllernavigationItem 的外观要灵活得多。

                          你为什么这么问?

                          原因很简单,navigationItem 与单个 UIViewController 相关联,并代表特定 UIViewControllernavigationBar 的状态。这很大,因为您不必处理 viewWillAppear(或类似的东西)内不同视图控制器之间的导航栏更改,就像您对 navigationBar 进行变异时那样;请记住,它在给定导航堆栈 (UINavigationController) 的所有视图控制器之间共享,并且在一个地方更改它会更改堆栈之前的所有视图控制器。

                          您只需为特定的视图控制器设置正确的UINavigationBarAppearance,UIKit 就会正确更新导航栏样式,具体取决于哪个视图控制器当前是导航堆栈上的顶部视图控制器。

                          navigationItem.standardAppearance` = `UINavigationBarAppearance()
                          

                          【讨论】:

                            【解决方案19】:

                            对于 iOS 13+,诀窍是使用 TransparentBackground 初始化 'UINavigationBarAppearance'。然后就可以轻松去掉导航栏的水平线了。

                            let appearance = UINavigationBarAppearance()
                            appearance.configureWithTransparentBackground()
                            appearance.backgroundColor = .green // Required background color
                            

                            最后,按照苹果的建议为导航项添加外观变化。

                            self.navigationItem.standardAppearance = appearance
                            self.navigationItem.scrollEdgeAppearance = appearance
                            self.navigationItem.compactAppearance = appearance
                            

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 1970-01-01
                              • 2019-08-05
                              • 1970-01-01
                              • 2014-01-27
                              • 2012-07-12
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              相关资源
                              最近更新 更多