如何快速删除NavigationBar的边框? [英] How to remove border of the navigationBar in swift?

查看:80
本文介绍了如何快速删除NavigationBar的边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试删除NavigationBars边框,但是没有运气.经过研究,人们似乎告诉我们将shadowImage和BackgroundImage设置为nil,但这在我的情况下不起作用.

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 work in my case.

我的代码

    self.navigationController?.navigationBar.barTintColor = UIColor(rgba: "#4a5866")
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: ""), forBarMetrics: UIBarMetrics.Default)
    self.navigationController?.navigationBar.shadowImage = UIImage(named: "")

插图:

推荐答案

问题出在这两行:

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

由于没有没有名称的图像,因此UIImage(named: "")返回nil,这意味着将启动默认行为:

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

为非零时,将显示自定义阴影图像,而不是默认阴影图像.为了显示自定义阴影,还必须使用-setBackgroundImage:forBarMetrics:设置自定义背景图像(如果使用默认背景图像,则将使用默认阴影图像).

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).

您需要一个真正为空的图像,因此只需使用UIImage()进行初始化:

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

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()

这篇关于如何快速删除NavigationBar的边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆