问题
I added a UINavigationController
to my UIViewController
programmatically like following:
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. 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
. Is it possible to remove/hide the _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. I would like to know If there is a way to hide/remove the BackdropView
instead of adding a UIView
on top of it.
回答1:
Officially, this is only possible by setting the shadowImage
of the navigationBar to an empty image, as has been mentioned in another answer. 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. 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. You can find it and set it as hidden. This is what Apple does in their native calendar app, for example. Just iterate the subviews, and set the image view to hidden.
回答2:
It is Apple private API. You can try to access it using superviews, or using valueForKey
. However, I would not recommend using that. First of all, Apple may reject your application. Moreover, structure of all Apple built views may change at any time and your application may face issues. More information about _UIBackdropView you can find here.
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.
来源:https://stackoverflow.com/questions/32208378/how-to-remove-the-shadow-line-from-a-navigation-bar