How to hide UINavigationBar 1px bottom line

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

    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
        }
    
    
    }
    

提交回复
热议问题