【问题标题】:How to remove or hide the toolbars' top shadow如何删除或隐藏工具栏的顶部阴影
【发布时间】:2012-12-24 07:06:52
【问题描述】:

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

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];

【问题讨论】:

  • 不要将您的问题编辑为答案,这不再有意义。回答您自己的问题很好(您也可以接受自己的回答)。
  • @jrturton 哦,好的,谢谢。我犹豫是否使用“回答我自己的问题”;不确定。 setShadowImage toolbar 的属性避开了我,因为它在 UIToolbar 之下;在开发者文档中。

标签: iphone ios uitoolbar


【解决方案1】:

其他答案均不适用于 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];
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      也加入这一行

      [toolbar setShadowImage:_maskedImage forToolbarPosition:UIToolbarPositionAny];
      

      两个重要说明:

      1. 您还必须设置背景图片,否则这将不起作用。
      2. 这适用于 iOS 6+

      【讨论】:

      • 您能否更正您的代码格式,使其出现在代码示例区域中。更容易阅读。
      【解决方案4】:

      首先在您的项目中添加QuartzCore/QuartzCore 框架,然后将其导入您的.m 文件中,如下所示...

      #import <QuartzCore/QuartzCore.h>
      

      然后添加下面的代码...

          yourToolBar.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
      
          yourToolBar.layer.shadowOpacity =0.0f;
      
          yourToolBar.layer.shadowRadius = 0.0f;
      

      希望对您有所帮助...

      【讨论】:

      • 据我所知,这对 iOS7 没有影响。
      【解决方案5】:

      在 iOS 7 中没有其他答案对我有用,所以这是我使用 Interface Builder 所做的:

      1. 将工具栏添加到 UIView。
      2. UIView 的大小与 UIToolbar 相同。
      3. 向下拖动 UIView 的顶部,直到它刚好覆盖 UIToolbar 的顶部。
      4. 使用属性检查器单击“剪辑子视图”。

      这样做会将工具栏的顶部剪掉,从而去除灰色阴影。

      【讨论】:

        【解决方案6】:

        setBackgroundImage:_maskedImage 工具栏隐藏后需要移除阴影或者调用自定义的阴影清除方法

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-12
          相关资源
          最近更新 更多