ios – UIBarButtonItem上没有阴影/浮雕

前端之家收集整理的这篇文章主要介绍了ios – UIBarButtonItem上没有阴影/浮雕前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有自定义UIBarButtonItem的问题.当我通过创建自定义UIBarButtonItem时
  1. [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"FilterIcon.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(filterTouched:)];

生成的按钮没有“浮雕”外观,系统项目通过在其图标后面放置一个半透明的黑色阴影来实现.

在左侧,您可以看到“组织”系统栏按钮项,右键是上面代码的结果.

在资源中创建阴影是徒劳的,因为iOS / Cocoa只使用图像的掩码并丢弃任何颜色信息.

有趣的是,如果我在Interface-Builder中创建条形按钮项,它看起来很好.但是,在我的问题的上下文中,我需要在代码中创建按钮项.

@R_502_323@

James Furey的剧本有Objective-C版本.
  1. - (UIImage *)applyToolbarButtonStyling:(UIImage *)oldImage {
  2. float shadowOffset = 1;
  3. float shadowOpacity = .54;
  4. CGRect imageRect = CGRectMake(0,oldImage.size.width,oldImage.size.height);
  5. CGRect shadowRect = CGRectMake(0,shadowOffset,oldImage.size.height);
  6. CGRect newRect = CGRectUnion(imageRect,shadowRect);
  7. UIGraphicsBeginImageContextWithOptions(newRect.size,NO,oldImage.scale);
  8. CGContextRef ctx = UIGraphicsGetCurrentContext();
  9. CGContextScaleCTM(ctx,1,-1);
  10. CGContextTranslateCTM(ctx,-(newRect.size.height));
  11. CGContextSaveGState(ctx);
  12. CGContextClipToMask(ctx,shadowRect,oldImage.CGImage);
  13. CGContextSetFillColorWithColor(ctx,[UIColor colorWithWhite:0 alpha:shadowOpacity].CGColor);
  14. CGContextFillRect(ctx,shadowRect);
  15. CGContextRestoreGState(ctx);
  16. CGContextClipToMask(ctx,imageRect,[UIColor colorWithWhite:1 alpha:1].CGColor);
  17. CGContextFillRect(ctx,imageRect);
  18. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  19. UIGraphicsEndImageContext();
  20. return newImage;
  21. }

猜你在找的iOS相关文章