最简单的半透明方法是 [toolBar setBarStyle:UIBarStyleBlackTranslucent]; 这样做的透明效果,有点深,没有达到我想要的效果,我希望能更浅一些。
![uitoolbar uitoolbar 半透明](http://iphone.xiaoxiaostudio.net/wp-content/uploads/2013/05/uitoolbar-300x40.jpg)
为了达到上面的效果,就要继承一个UIToolbar重写initWithFrame:(CGRect)aRect 这个方法,然后再在UIToolbar上面放一个UIView,就可以任意指定透明度了。 @interface MyToolbar : UIToolbar { } @end @implementation MyToolbar - (void)drawRect:(CGRect)rect { // do nothing } - (id)initWithFrame:(CGRect)aRect { if ((self = [super initWithFrame:aRect])) { self.opaque = NO; self.backgroundColor = [UIColor clearColor]; self.clearsContextBeforeDrawing = YES; } return self; } @end 下面是使用这个MyToolbar MyToolbar * toolBar=[[MyToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; //添加指定透明度的UIView UIView * backView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; [backView setBackgroundColor:[UIColor blackColor]]; [backView setAlpha:0.3]; [toolBar addSubview:backView];