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

How to remove the shadow line from a navigation bar?

我以编程方式向我的 UIViewController 添加了一个 UINavigationController,如下所示:

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

在调试视图层次结构中,我看到 UINavigationBar 后面有一个名为 UIBackdropView 的视图,背景为灰色。您可以在屏幕截图中看到:

我试图在文档中找到它,但找不到。

我们可以访问这一层吗?

这会导致 UINavigationBar 下方出现一条 1 像素的灰线。是否可以 remove/hide _UIBackDropView?

我试图通过在 UINavigationBar 下添加一个简单的 UIView 和白色背景颜色来混合这条 1 像素的线,它覆盖了这条暗线,并且效果很好。我想知道是否有办法 hide/remove BackdropView 而不是在其上添加 UIView

它是 Apple 私有 API。您可以尝试使用超级视图或使用 valueForKey 来访问它。但是,我不建议使用它。首先,苹果可能会拒绝你的申请。此外,所有 Apple 构建的视图的结构都可能随时发生变化,您的应用程序可能会面临问题。有关 _UIBackdropView 的更多信息,您可以找到 here.

删除一个像素下划线的最简单、最快捷的解决方案是:

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

但是它会消除模糊,这可能不是您想要的。您可以使用 UINavigationBar-Addition 轻松去除细线而不影响您的模糊。

正式来说,这只能通过将导航栏的 shadowImage 设置为空图像来实现,正如另一个答案中提到的那样。但是,仔细查看文档,是这样说的:

For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

通过使用自定义背景图片,您会失去模糊的半透明背景,这可能不是您想要的。

"hairline" 是一个 UIImageView,它是导航栏的子视图。您可以找到它并将其设置为隐藏。例如,这就是 Apple 在其原生日历应用程序中所做的。只需迭代子视图,并将图像视图设置为隐藏。