How to remove border of the navigationBar in swift?

前端 未结 25 2063
执笔经年
执笔经年 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: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()
    }
    

提交回复
热议问题