2

次のコードを使用して、navbar のカスタム右バー ボタン項目を作成しています。

// create a toolbar 
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 50, 44.01)];
// create the array to hold the button, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:1];

// create a toolbar item with image
NSString* pathToSettingsResourceFile = [[NSBundle mainBundle] pathForResource:@"19-gear" ofType:@"png"];        
UIImage* settingsImage = [[[UIImage alloc] initWithContentsOfFile:pathToSettingsResourceFile] autorelease];         
settings = [[UIBarButtonItem alloc]initWithImage:settingsImage style:UIBarButtonItemStylePlain target:self action:@selector(showSettings:)];
settings.enabled = FALSE;

// add button to toolbar 
[buttons addObject:settings];
[tools setItems:buttons animated:NO];
[buttons release];

// add toolbar as a right bar button item 
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:tools] autorelease];

[tools release];   

iOS 6までこれは期待どおりに機能し、iOS 6からはこの奇妙な長方形の背景が表示されます

右バーボタン項目の設定

と追加

tools.opaque = NO;
tools.backgroundColor = [UIColor clearColor];

役に立たない、

この背景を取り除くのを手伝ってください

前もってありがとう、アミット

4

1 に答える 1

1

これが古いスレッドであることは知っていますが、同じ問題があり、私が発見したことに興味があるかもしれないと思いました.

このスレッドで回避策を利用できます(「受け入れられた」回答ではなく、jd による回答)。

次のコードで、必要な動作を得ることができるはずです。

const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];

[tools setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

これは機能しますが、それが正しい方法であるとは確信していません。そのため、含まれる四角形を透明なオブジェクトとして動作させるための秘密のコードを誰かが知っている場合に備えて、ここで新しいスレッドを開始しました。UIToolbar

于 2013-05-17T22:18:05.570 に答える