How to hide UINavigationBar 1px bottom line

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

    Two lines solution that works for me. Try to add this in ViewDidLoad method:

    navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
    self.extendedLayoutIncludesOpaqueBars = true
    
    0 讨论(0)
  • 2020-11-22 09:25

    Wanted to add the Swift version of Serhii's answer. I created a UIBarExtension.swift with the following:

    import Foundation
    import UIKit
    
    extension UINavigationBar {
        func hideBottomHairline() {
            self.hairlineImageView?.isHidden = true
        }
    
        func showBottomHairline() {
            self.hairlineImageView?.isHidden = false
        }
    }
    
    extension UIToolbar {
        func hideBottomHairline() {
            self.hairlineImageView?.isHidden = true
        }
    
        func showBottomHairline() {
            self.hairlineImageView?.isHidden = false
        }
    }
    
    extension UIView {
        fileprivate var hairlineImageView: UIImageView? {
            return hairlineImageView(in: self)
        }
    
        fileprivate func hairlineImageView(in view: UIView) -> UIImageView? {
            if let imageView = view as? UIImageView, imageView.bounds.height <= 1.0 {
                return imageView
            }
    
            for subview in view.subviews {
                if let imageView = self.hairlineImageView(in: subview) { return imageView }
            }
    
            return nil
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:27
    Slightly Swift Solution 
    func setGlobalAppearanceCharacteristics () {
        let navigationBarAppearace = UINavigationBar.appearance()
        navigationBarAppearace.tintColor = UIColor.white
        navigationBarAppearace.barTintColor = UIColor.blue
        navigationBarAppearace.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        navigationBarAppearace.shadowImage = UIImage()
    
    }
    
    0 讨论(0)
  • 2020-11-22 09:30

    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:

    [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
                                      forBarPosition:UIBarPositionAny
                                          barMetrics:UIBarMetricsDefault];
    
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
    
    0 讨论(0)
  • 2020-11-22 09:31

    pxpgraphics' solution updated for Swift 2.0

    extension UINavigationBar {
    
        func hideBottomHairline()
        {
            hairlineImageViewInNavigationBar(self)?.hidden = true
        }
    
        func showBottomHairline()
        {
            hairlineImageViewInNavigationBar(self)?.hidden = false
        }
    
        private func hairlineImageViewInNavigationBar(view: UIView) -> UIImageView?
        {
            if let imageView = view as? UIImageView where imageView.bounds.height <= 1
            {
                return imageView
            }
    
            for subview: UIView in view.subviews
            {
                if let imageView = hairlineImageViewInNavigationBar(subview)
                {
                    return imageView
                }
            }
    
            return nil
        }
    
    }
    
    extension UIToolbar
    {
    
        func hideHairline()
        {
            let navigationBarImageView = hairlineImageViewInToolbar(self)?.hidden = true
        }
    
        func showHairline()
        {
            let navigationBarImageView = hairlineImageViewInToolbar(self)?.hidden = false
        }
    
        private func hairlineImageViewInToolbar(view: UIView) -> UIImageView?
        {
            if let imageView = view as? UIImageView where imageView.bounds.height <= 1
            {
                return imageView
            }
    
            for subview: UIView in view.subviews
            {
                if let imageView = hairlineImageViewInToolbar(subview)
                {
                    return imageView
                }
            }
    
            return nil
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 09:32

    Below can help simply!

    Swift:

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

    Objective C:

    [self.navigationController.navigationBar setValue:@(YES) forKeyPath:@"hidesShadow"];
    
    0 讨论(0)
提交回复
热议问题