我有一个自定义UIBarButtonItem的问题。当我通过创建自定义UIBarButtonItem时
[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"FilterIcon.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(filterTouched:)];生成的按钮没有“浮雕”外观,系统项通过在其图标后面放置半透明的黑色阴影来实现。

在左边你可以看到“组织”系统栏按钮项,右边是上面代码的结果。
在资源中创建阴影是徒劳的,因为iOS/Cocoa只使用了图像的掩码,并丢弃了任何颜色信息。
有趣的是,如果我在Interface-Builder中创建栏按钮项,它看起来很好。但是,在我的问题上下文中,我需要在代码中创建按钮项。
发布于 2012-08-08 21:21:52
这里有James Furey的脚本的Objective-C版本。
- (UIImage *)applyToolbarButtonStyling:(UIImage *)oldImage {
float shadowOffset = 1;
float shadowOpacity = .54;
CGRect imageRect = CGRectMake(0, 0, oldImage.size.width, oldImage.size.height);
CGRect shadowRect = CGRectMake(0, shadowOffset, oldImage.size.width, oldImage.size.height);
CGRect newRect = CGRectUnion(imageRect, shadowRect);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, oldImage.scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -(newRect.size.height));
CGContextSaveGState(ctx);
CGContextClipToMask(ctx, shadowRect, oldImage.CGImage);
CGContextSetFillColorWithColor(ctx, [UIColor colorWithWhite:0 alpha:shadowOpacity].CGColor);
CGContextFillRect(ctx, shadowRect);
CGContextRestoreGState(ctx);
CGContextClipToMask(ctx, imageRect, oldImage.CGImage);
CGContextSetFillColorWithColor(ctx, [UIColor colorWithWhite:1 alpha:1].CGColor);
CGContextFillRect(ctx, imageRect);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}发布于 2012-06-29 17:02:01
我认为发生这种情况的原因可以通过以下对另一个问题的回答来解释:
https://stackoverflow.com/a/3476424/1210490
https://stackoverflow.com/a/6528603/1210490
根据您以编程方式附加它们的位置,UIBarButtonItems的行为会有所不同。如果你把它们连接到工具栏上,它们就会变成白色的“浮雕”图标。如果您将它们附加到导航栏,它们将不会。
我花了几个小时编写了一个函数,将工具栏UIBarButtonItem样式应用到UIImages。它是用C#为MonoTouch写的,但我相信你能把它改成Obj-C没问题的……
UIImage ApplyToolbarButtonStyling(UIImage oldImage)
{
float shadowOffset = 1f;
float shadowOpacity = .54f;
RectangleF imageRect = new RectangleF(PointF.Empty, oldImage.Size);
RectangleF shadowRect = new RectangleF(new PointF(0, shadowOffset), oldImage.Size);
RectangleF newRect = RectangleF.Union(imageRect, shadowRect);
UIGraphics.BeginImageContextWithOptions(newRect.Size, false, oldImage.CurrentScale);
CGContext ctxt = UIGraphics.GetCurrentContext();
ctxt.ScaleCTM(1f, -1f);
ctxt.TranslateCTM(0, -newRect.Size.Height);
ctxt.SaveState();
ctxt.ClipToMask(shadowRect, oldImage.CGImage);
ctxt.SetFillColor(UIColor.FromWhiteAlpha(0f, shadowOpacity).CGColor);
ctxt.FillRect(shadowRect);
ctxt.RestoreState();
ctxt.ClipToMask(imageRect, oldImage.CGImage);
ctxt.SetFillColor(UIColor.FromWhiteAlpha(1f, 1f).CGColor);
ctxt.FillRect(imageRect);
UIImage newImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return newImage;
}所以,一个曾经看起来像这样的UIBarButtonItem:

而是使用上面的函数创建,如下所示:
UIBarButtonItem barButtonItem = new UIBarButtonItem(ApplyToolbarButtonStyling(UIImage.FromFile("MusicIcon.png")), UIBarButtonItemStyle.Plain, delegate {});现在看起来像这样:

希望这对将来的人有所帮助。
发布于 2013-04-11 23:18:45
请注意James Furey脚本中的阴影偏移。我有以下经验:
float shadowOffset = 1.0f // for a UIBarButtonItem in UINavigationItem
float shadowOffset = 0.0f // for a UIBarButtonItem in UIToolBar这是通过iOS 6.1SDK观察到的。
(现在在iOS 7下已过时)
https://stackoverflow.com/questions/11083335
复制相似问题