How to hide UINavigationBar 1px bottom line

后端 未结 30 2222
不知归路
不知归路 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:32

    The swift way to do it:

    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
    UINavigationBar.appearance().shadowImage = UIImage()
    
    0 讨论(0)
  • 2020-11-22 09:35

    Swift put this

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

    in

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    
    0 讨论(0)
  • 2020-11-22 09:36

    I use a UINavigationBar extension that enables me to hide/show that shadow using the UIAppearance API or selecting which navigation bar has to hide/show that shadow using Storyboard (or source code). Here is the extension:

    import UIKit
    
    private var flatAssociatedObjectKey: UInt8 = 0
    
    /*
      An extension that adds a "flat" field to UINavigationBar. This flag, when
      enabled, removes the shadow under the navigation bar.
     */
    @IBDesignable extension UINavigationBar {
        @IBInspectable var flat: Bool {
            get {
                guard let obj = objc_getAssociatedObject(self, &flatAssociatedObjectKey) as? NSNumber else {
                    return false
                }
                return obj.boolValue;
            }
    
            set {
                if (newValue) {
                    let void = UIImage()
                    setBackgroundImage(void, forBarPosition: .Any, barMetrics: .Default)
                    shadowImage = void
                } else {
                    setBackgroundImage(nil, forBarPosition: .Any, barMetrics: .Default)
                    shadowImage = nil
                }
                objc_setAssociatedObject(self, &flatAssociatedObjectKey, NSNumber(bool: newValue),
                        objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
        }
    }
    

    Now, to disable the shadow across all navigation bars you have to use:

    UINavigationBar.appearance().flat = true
    

    Or you can enable/disable this behavior using storyboards:

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

    Try this:

    [[UINavigationBar appearance] setBackgroundImage: [UIImage new]  
                                       forBarMetrics: UIBarMetricsDefault];
    
    [UINavigationBar appearance].shadowImage = [UIImage new];
    

    Below image has the explanation (iOS7 NavigationBar):

    enter image description here

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

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

    Simple solution in swift

       let navigationBar = self.navigationController?.navigationBar
        navigationBar?.setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
        navigationBar?.shadowImage = UIImage()
    
    0 讨论(0)
  • 2020-11-22 09:39

    Swift 4 Tested ONE LINE SOLUTION

    In Viewdidload() Set Navigation controller's userdefault value true for key "hidesShadow"

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
    
    }
    
    0 讨论(0)
提交回复
热议问题