2013-04-26 7 views
5

UIToolBarに透明な背景(iBooksに似ています)を作成したいのですが、translucentプロパティを設定することで幸運を祈ることはありません。UIToolbarの背景を透明にするには?

ここに私のコードです:

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    NSMutableArray *toolBarItems = [[NSMutableArray alloc] init]; 
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]]; 
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Source" style:UIBarButtonItemStyleBordered target:nil action:nil]]; 
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Aa" style:UIBarButtonItemStyleBordered target:nil action:nil]]; 
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Rabbit" style:UIBarButtonItemStyleBordered target:nil action:nil]]; 
    toolBar.items = toolBarItems; 
    toolBar.translucent = YES; 
    [self.view addSubview:toolBar]; 

それはまだこのように出てくる:

enter image description here

+1

これを行うには、透明な背景画像を設定する必要があると思います。ちょうど1x1ピクセルの透明なpngを作成してください。 – MTurner

+0

@doug Smithhのようにuitoolbarはuiviewのサブクラスです。レイヤプロパティを使用して、アルファの値を変更して透明にすることができます –

+0

をご覧ください: http://stackoverflow.com/questions/2468831/couldnt-uitoolbar-透明にする#答え-3253738 – Shad

答えて

23

透明:

[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; 

、あなたは半透明としてツールバーたい場合:

[toolBar setBarStyle:UIBarStyleBlack]; 
toolBar.translucent = YES; 

は、それはあなたのお役に立てば幸いです。

+0

透明に使用した方法を説明できますか? –

+1

透明なメソッドは、ツールバーの境界線の上部に薄いバーを表示しますが、これを取り除く方法は? – Malloc

+1

iOS 7用の洗練された洗練されたソリューション –

2

1つのオプションは、ボタンは自分自身を描画していきます、UIToolbarをサブクラス化し、drawメソッドをオーバーライドすることです通常どおり:

@interface TransparentToolbar : UIToolbar 
{ 
} 

@implementation TransparentToolbar 

// drawRect stub, toolbar items will still draw themselves 
- (void)drawRect:(CGRect)rect 
{ 
    return; 
} 

@end