How to remove border of the navigationBar in swift?

前端 未结 25 1741
执笔经年
执笔经年 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:24

    this is the answer in swift 3 base of Nate Cook answer

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

    Just write this in the extension of UINavigationBar

    extension UINavigationBar {
    
        func shouldRemoveShadow(_ value: Bool) -> Void {
            if value {
                self.setValue(true, forKey: "hidesShadow")
            } else {
                self.setValue(false, forKey: "hidesShadow")
            }
        }
    }
    

    And in your viewController...

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.shouldRemoveShadow(true)        
    }
    

    And to get this undone for any viewController, just pass false.

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

    in your custom navigationController add these lines:

    self.navigationBar.setBackgroundImage(UIImage(), for:.default)
    self.navigationBar.shadowImage = UIImage()
    self.navigationBar.layoutIfNeeded()
    

    Important Note

    the last line is important if you use the first line viewDidLoad() method because navigationController should redraw nav bar but easily you can use this without layoutIfNeeded() in the viewWillAppear() method before it draws the nav bar

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

    iOS 11 and Swift 4 You should try following if you want to remove the border but don't to make the navigitonbar translucent
    self.navigationBar.shadowImage = UIImage()

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

    The trouble is with these two lines:

    self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: ""), forBarMetrics: UIBarMetrics.Default)
    self.navigationController?.navigationBar.shadowImage = UIImage(named: "")
    

    Since you don't have an image with no name, UIImage(named: "") returns nil, which means the default behavior kicks in:

    When non-nil, a custom shadow image to show instead of the default shadow image. For a custom shadow to be shown, a custom background image must also be set with -setBackgroundImage:forBarMetrics: (if the default background image is used, the default shadow image will be used).

    You need a truly empty image, so just initialize with UIImage():

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

    The accepted answer worked for me but I noticed when I wanted the shadow image to reappear when popping back or pushing forward to another vc there was a noticeable blink in the navigation bar.

    Using this method navigationController?.navigationBar.setValue(true, forKey: "hidesShadow") in viewWillAppear the shadow bar is hidden in the current visible view controller.

    Using these 2 methods

    navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
    navigationController?.navigationBar.setValue(false, forKey: "hidesShadow")
    

    in viewWillDisappear the blink still happens but only when the shadow image reappears and not the navigation bar itself.

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        // 1. hide the shadow image in the current view controller you want it hidden in
        navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
        navigationController?.navigationBar.layoutIfNeeded()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(true)
    
        // 2. show the shadow image when pushing or popping in the next view controller. Only the shadow image will blink
        navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
        navigationController?.navigationBar.setValue(false, forKey: "hidesShadow")
        navigationController?.navigationBar.layoutIfNeeded()
    }
    
    0 讨论(0)
提交回复
热议问题