6

カスタム UIBarButtonItem に問題があります。経由でカスタム UIBarButtonItem を作成すると

 [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"FilterIcon.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(filterTouched:)];

結果として得られるボタンには、システム アイテムがアイコンの後ろに半透明の黒い影を配置することによって実現する「浮き彫り」の外観がありません。

左:システムアイテム、右:カスタムアイテム

左側に「Organize」システム バー ボタン項目が表示され、右側に上記のコードの結果が表示されます。

iOS/Cocoa は画像のマスクのみを使用し、色情報を破棄するため、リソースに影を作成しても無駄です。

興味深いことに、Interface-Builder でバー ボタン アイテムを作成すると、問題なく表示されます。ただし、私の問題のコンテキストでは、ボタン項目をコードで作成する必要があります。

4

3 に答える 3

10

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-08-08T13:21:51.597 に答える
7

これが発生する理由は、別の質問に対するこれらの回答でカバーされていると思います。

https://stackoverflow.com/a/3476424/1210490

https://stackoverflow.com/a/6528603/1210490

UIBarButtonItem の動作は、プログラムでどこにアタッチするかによって異なります。それらをツールバーに添付すると、白い「エンボス」アイコンになります。それらをナビゲーション バーにアタッチすると、アタッチされません。

ここ数時間かけて、ツールバーの UIBarButtonItem スタイルを UIImage に適用する関数を書きました。MonoTouch 用に C# で記述されていますが、問題なく 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 {});

次のようになります。

後

これが将来誰かに役立つことを願っています。

于 2012-06-29T09:02:00.857 に答える