如何删除或隐藏工具栏的顶部阴影

cod*_*ind 6 iphone uitoolbar ios

删除工具栏的背景后,使用图像蒙版,工具栏上方仍保留阴影线.我们如何摆脱它?如您所见,通过下图,我想使用工具栏和按钮,但没有背景或顶部阴影.

const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *_img = [[UIImage alloc] init];
UIImage *_maskedImage = [UIImage imageWithCGImage:CGImageCreateWithMaskingColors(_img.CGImage, colorMask)];
[self.navigationController.toolbar setBackgroundImage:_maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

在隐藏与图象面具的背景以后的工具栏顶面阴影

nat*_*bro 15

其他答案都没有在iOS7上运行,有些似乎也不适用于较旧的iOS版本.这个(释义http://samplecodebank.blogspot.com/2013/06/UIToolbar-setShadowImage-forToolbarPosition-example.html)对我来说在5.1+上一致,并且比生成自定义背景图像和颜色蒙版更简洁,更高效.

toolbar.backgroundColor = [UIColor clearColor];
if ([toolbar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
  [toolbar setBackgroundImage:[UIImage new] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
}
if ([toolbar respondsToSelector:@selector(setShadowImage:forToolbarPosition:)]) {
  [toolbar setShadowImage:[UIImage new] forToolbarPosition:UIToolbarPositionAny];
}
Run Code Online (Sandbox Code Playgroud)


rei*_*man 7

在iOS 7上,设置[toolBar setClipsToBounds:YES].


小智 5

也添加这一行

[toolbar setShadowImage:_maskedImage forToolbarPosition:UIToolbarPositionAny];
Run Code Online (Sandbox Code Playgroud)

两个重要说明:

  1. 您还必须设置背景图像,否则不会执行任何操作.
  2. 这适用于iOS 6+