How to remove border of the navigationBar in swift?

前端 未结 25 1831
执笔经年
执笔经年 2020-12-02 04:41

i\'ve been trying to remove the navigationBars border without luck. I\'ve researched and people seem to tell to set shadowImage and BackgroundImage to nil, but this does not

相关标签:
25条回答
  • 2020-12-02 05:17

    Only this worked for me,

    self.navigationController?.navigationBar.shadowImage = UIImage()
    

    Ref

    0 讨论(0)
  • 2020-12-02 05:20

    Set barStyle to .Black before setting the tint:

    self.navigationController?.navigationBar.translucent = false
    self.navigationController?.navigationBar.barStyle = .Black
    self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()
    
    0 讨论(0)
  • 2020-12-02 05:20

    Swiftier method of Jack Chen:

    extension UINavigationController {
    
        var isHiddenHairline: Bool {
            get {
                guard let hairline = findHairlineImageViewUnder(navigationBar) else { return true }
                return hairline.isHidden
            }
            set {
                if let hairline = findHairlineImageViewUnder(navigationBar) {
                    hairline.isHidden = newValue
                }
            }
        }
    
        private func findHairlineImageViewUnder(_ view: UIView) -> UIImageView? {
            if view is UIImageView && view.bounds.size.height <= 1.0 {
                return view as? UIImageView
            }
    
            for subview in view.subviews {
                if let imageView = self.findHairlineImageViewUnder(subview) {
                    return imageView
                }
            }
    
            return nil
        }
    }
    

    Using:

        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            navigationController?.isHiddenHairline = true
        }
    
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            navigationController?.isHiddenHairline = false
        }
    
    0 讨论(0)
  • 2020-12-02 05:20

    for the swift3 you should write slightly different way:

     self.navigationController?.navigationBar.setBackgroundImage(UIImage(),
        for: UIBarMetrics.default)
       self.navigationController?.navigationBar.shadowImage = UIImage()
    
    0 讨论(0)
  • 2020-12-02 05:20

    The border line is an UIImageView and removing a subview which is an imageView will remove barButtonItems with UIImageView. Below code will help you remove it. Hope this helps someone who faced an issue like me.

    for parent in self.navigationController!.navigationBar.subviews {
            for childView in parent.subviews {
                if childView.frame.height == 0.5 {
                    childView.removeFromSuperview()
                }
            }
        }
    

    The border UIImageView is only 0.5 in height so this code removes only that.

    0 讨论(0)
  • 2020-12-02 05:23

    Updated for Swift 4 in case someone is wondering

    navigationBar.shadowImage = UIImage()
    navigationBar.backIndicatorImage = UIImage()
    

    It's even less verbose now.

    0 讨论(0)
提交回复
热议问题