简体   繁体   English

如何从导航栏中删除阴影线?

[英]How to remove the shadow line from a navigation bar?

I added a UINavigationController to my UIViewController programmatically like following: 我以编程方式将UINavigationController添加到了UIViewController如下所示:

 UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:[ViewController fromStoryboardNamed:@"myVC"]];

and in the debug view hierarchy I see there is a view called UIBackdropView behind the UINavigationBar with gray color background. 在调试视图层次结构中,我看到UINavigationBar后面有一个名为UIBackdropView的视图,背景为灰色。 You can see that in the screenshot: 您可以在屏幕截图中看到:

在此处输入图片说明

I tried to find it in the Documentation,but I couldn't find any. 我试图在文档中找到它,但是找不到。

Can we access this layer? 我们可以进入这一层吗?

This causes a 1 pixel gray line to appear under the UINavigationBar . 这将导致1像素的灰线出现在UINavigationBar Is it possible to remove/hide the _UIBackDropView ? 是否可以删除/隐藏_UIBackDropView

I tried to blend this 1 pixel line by adding a simple UIView with white background color under the UINavigationBar which covers this dark line,and it worked fine. 我试图通过在覆盖了该暗线的UINavigationBar下添加带有白色背景色的简单UIView来混合这1像素线,并且效果很好。 I would like to know If there is a way to hide/remove the BackdropView instead of adding a UIView on top of it. 我想知道是否有一种隐藏/删除BackdropView而不是在其顶部添加UIView的方法。

Officially, this is only possible by setting the shadowImage of the navigationBar to an empty image, as has been mentioned in another answer. 正式地,这只能通过将navigationBar的shadowImage设置为空图像来实现,如另一个答案中所述。 However, a closer look at the documentation, it is said: 但是,仔细看一下文档,据说:

For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. 为了显示自定义阴影图像,还必须使用setBackgroundImage:forBarMetrics:方法设置自定义背景图像。 If the default background image is used, then the default shadow image will be used regardless of the value of this property. 如果使用默认的背景图像,则无论此属性的值如何,都将使用默认的阴影图像。

By using a custom background image, you would lose the blurred background translucency, which is likely not what you want. 通过使用自定义背景图像,您将失去模糊的背景半透明性,这可能不是您想要的。

The "hairline" is a UIImageView that is a subview of the navigation bar. “发际线”是UIImageView,它是导航栏的子视图。 You can find it and set it as hidden. 您可以找到它并将其设置为隐藏。 This is what Apple does in their native calendar app, for example. 例如,这就是Apple在其本机日历应用程序中所做的。 Just iterate the subviews, and set the image view to hidden. 只需迭代子视图,然后将图像视图设置为隐藏即可。

It is Apple private API. 它是Apple专用API。 You can try to access it using superviews, or using valueForKey . 您可以尝试使用超级视图或使用valueForKey对其进行访问。 However, I would not recommend using that. 但是,我不建议您使用它。 First of all, Apple may reject your application. 首先,Apple可能会拒绝您的申请。 Moreover, structure of all Apple built views may change at any time and your application may face issues. 此外,所有Apple构建的视图的结构可能随时更改,并且您的应用程序可能会遇到问题。 More information about _UIBackdropView you can find here . 有关_UIBackdropView的更多信息,请参见 此处

The easiest and quickest solution to remove one pixel underline is this one: 删除一个像素下划线的最简单,最快捷的解决方案是:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

However it will remove blur, what is probably not what you want. 但是,它将消除模糊,这可能不是您想要的。 You can use UINavigationBar-Addition to easily remove hairline without affect your blur. 您可以使用UINavigationBar-Addition轻松删除发际线,而不会影响模糊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM