How to hide UINavigationBar 1px bottom line

后端 未结 30 2174
不知归路
不知归路 2020-11-22 08:58

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 ba

相关标签:
30条回答
  • 2020-11-22 09:17

    Simple solution – Swift 5

    1. Create an extension:

      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. Add this to viewDidLoad():

      self.navigationController?.navigationBar.shadowImage = UIImage.hideNavBarLine(color: UIColor.clear)
      
    0 讨论(0)
  • 2020-11-22 09:17

    You should add a view to a bottom of the 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)
    
    0 讨论(0)
  • 2020-11-22 09:17

    I Just created an extension for this... Sorry about formatting (this is my first answer).

    Usage:

      override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.hideShadow = true
    }
    

    Extension:

     UINavigationController.swift
    //  Created by Ricardo López Rey on 16/7/15.
    
    import Foundation
    
    
    struct UINavigationControllerExtension {
        static var hideShadowKey : String = "HideShadow"
    static let backColor = UIColor(red: 247/255, green: 247/255, blue: 248/255, alpha: 1.0)
    }
    
    extension UINavigationController {
    
        var hideShadow : Bool {
            get {
                if let ret =  objc_getAssociatedObject(self, &UINavigationControllerExtension.hideShadowKey) as? Bool {
                    return ret
                } else {
                    return false
                }
    
    
            }
            set {
                objc_setAssociatedObject(self,&UINavigationControllerExtension.hideShadowKey,newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
    
                if newValue {
    
    
                self.navigationBar.setBackgroundImage(solidImage(UINavigationControllerExtension.backColor), forBarMetrics: UIBarMetrics.Default)
    
                    self.navigationBar.shadowImage = solidImage(UIColor.clearColor())
                } else {
                    self.navigationBar.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default)
                }
            }
        }
    
        private func solidImage(color: UIColor, size: CGSize = CGSize(width: 1,height: 1)) -> UIImage {
            var rect = CGRectMake(0, 0, size.width, size.height)
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            color.setFill()
            UIRectFill(rect)
            var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
    
    
    }
    
    0 讨论(0)
  • 2020-11-22 09:18

    Can also be hidden from Storyboard (working on Xcode 10.1)

    By adding runtime attribute: hidesShadow - Boolean - True

    0 讨论(0)
  • 2020-11-22 09:19

    Here's a very simple solution:

    self.navigationController.navigationBar.clipsToBounds = YES;
    
    0 讨论(0)
  • 2020-11-22 09:23

    I know this is an old thread, but I found a solution that works really well:

    Subclass UINavigationBar. In your UINavigationBar subclass, override didAddSubview with the following code:

    - (void)didAddSubview:(UIView *)subview
    {
        [super didAddSubview:subview];
    
        if ([subview isKindOfClass:[UIImageView class]]) {
            [subview setClipsToBounds:YES];
        }
    }
    
    0 讨论(0)
提交回复
热议问题