2013-04-26 35 views
5

Tôi muốn làm cho UIToolBar của tôi có nền trong suốt (tương tự như iBooks) nhưng tôi không có may mắn khi thiết lập thuộc tính translucent.Làm thế nào để làm cho nền của UIToolbar trong suốt?

Dưới đây là mã của tôi:

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]; 

Nó vẫn đi ra như thế này:

enter image description here

+1

Tôi tin rằng bạn sẽ cần đặt hình nền trong suốt để thực hiện việc này. Chỉ cần tạo một pixel png trong suốt 1x1 pixel. – MTurner

+0

@doug Smithh là uitoolbar là một phân lớp của uiview bạn có thể sử dụng thuộc tính lớp của nó để làm cho nó trong suốt bằng cách thay đổi giá trị alpha –

+0

Hãy xem: http://stackoverflow.com/questions/2468831/couldnt-uitoolbar- minh bạch # answer-3253738 – Shad

Trả lời

23

Nếu bạn muốn thanh công cụ Transparent:

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

và nếu bạn muốn thanh công cụ như Translucent:

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

Hy vọng nó sẽ giúp bạn.

+0

Bạn có thể giải thích phương pháp bạn đã sử dụng để minh bạch không? –

+1

Phương pháp minh bạch vẫn hiển thị thanh mỏng ở phía trên cùng của thanh công cụ, cách loại bỏ điều đó? – Malloc

+1

Giải pháp thanh lịch tuyệt vời cho iOS 7 –

2

Một lựa chọn là để phân lớp UIToolbar và ghi đè lên các phương pháp bốc thăm, các nút sẽ tiếp tục vẽ bản thân như bình thường:

@interface TransparentToolbar : UIToolbar 
{ 
} 

@implementation TransparentToolbar 

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

@end 
Các vấn đề liên quan