How to remove border of the navigationBar in swift?

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

提交回复
热议问题