游.程 2013-10-07 14:04 采纳率: 0%
浏览 724
已采纳

如何隐藏 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?

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

enter image description here

转载于:https://stackoverflow.com/questions/19226965/how-to-hide-uinavigationbar-1px-bottom-line

  • 写回答

30条回答 默认 最新

  • 乱世@小熊 2013-10-07 14:14
    关注

    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:

    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.

    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.

    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.

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

    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/viewWillDisappear methods:

      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 :)

    Many thanks to @Leo Natan for the original idea!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理