我尝试以下代码,但它不起作用.
[helloToolbar setBackgroundColor:[UIColor clearColor]];
morais.. 57
要制作完全透明的工具栏,请使用此处描述的方法.简而言之,创建一个TransparentToolbar
继承自UIToolbar 的新类,并使用它来代替UIToolbar.
TransarentToolbar.h
@interface TransparentToolbar : UIToolbar @end
TransarentToolbar.m
@implementation TransparentToolbar // Override draw rect to avoid // background coloring - (void)drawRect:(CGRect)rect { // do nothing in here } // Set properties to make background // translucent. - (void) applyTranslucentBackground { self.backgroundColor = [UIColor clearColor]; self.opaque = NO; self.translucent = YES; } // Override init. - (id) init { self = [super init]; [self applyTranslucentBackground]; return self; } // Override initWithFrame. - (id) initWithFrame:(CGRect) frame { self = [super initWithFrame:frame]; [self applyTranslucentBackground]; return self; } @end
(上面链接的博客文章中的代码)
要制作完全透明的工具栏,请使用此处描述的方法.简而言之,创建一个TransparentToolbar
继承自UIToolbar 的新类,并使用它来代替UIToolbar.
TransarentToolbar.h
@interface TransparentToolbar : UIToolbar @end
TransarentToolbar.m
@implementation TransparentToolbar // Override draw rect to avoid // background coloring - (void)drawRect:(CGRect)rect { // do nothing in here } // Set properties to make background // translucent. - (void) applyTranslucentBackground { self.backgroundColor = [UIColor clearColor]; self.opaque = NO; self.translucent = YES; } // Override init. - (id) init { self = [super init]; [self applyTranslucentBackground]; return self; } // Override initWithFrame. - (id) initWithFrame:(CGRect) frame { self = [super initWithFrame:frame]; [self applyTranslucentBackground]; return self; } @end
(上面链接的博客文章中的代码)
在iOS 5中,只需调用setBackgroundImage并传递透明图像.
这是我的方式(我动态生成透明图像):
CGRect rect = CGRectMake(0, 0, 1, 1); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]); CGContextFillRect(context, rect); UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [toolbar setBackgroundImage:transparentImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
你能做的最好就是使用
[helloToolbar setBarStyle:UIBarStyleBlack]; [helloToolbar setTranslucent:YES];
这将为您提供一个黑色但半透明的工具栏.
透明(iOS 5.0):
[toolbar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
半透明:
[toolbar setBarStyle:UIBarStyleBlack]; [toolbar setTranslucent:YES];
所有设备的累积解决方案,从最古老的iOS 3.0(iPhone 1)到最新的iOS 6.1(iPad mini).
@implementation UIToolbar (Extension) - (void)drawRect:(CGRect)rect { if (CGColorGetAlpha(self.backgroundColor.CGColor) > 0.f) { [super drawRect:rect]; } } - (void)setTransparent { //iOS3+ self.backgroundColor = [UIColor clearColor]; //iOS5+ if ([self respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) { [self setBackgroundImage:[[UIImage new] autorelease] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; } //iOS6+ if ([self respondsToSelector:@selector(setShadowImage:forToolbarPosition:)]) { [self setShadowImage:[[UIImage new] autorelease] forToolbarPosition:UIToolbarPositionAny]; } } @end
如果需要透明工具栏,请调用setTransparent
它.如果需要非透明工具栏,请设置您选择的backgroundColor或自行添加imageView.
另一种解决方案是为UIToolbar定义一个类别:
@implementation UIToolbar(Transparent) -(void)drawRect:(CGRect)rect { // do nothing in here } @end
在IB中,将工具栏设置为黑色半透明且不透明.