今天需要创建一个透明的UIToolbar,便看了下SDK,使用下面代码
CGRect barFrame = CGRectMake(900, 0, 124, 40); UIToolbar *tb; tb = [[UIToolbar alloc]initWithFrame:barFrame]; tb.barStyle =UIBarStyleDefault; tb.tintColor = [UIColor clearColor]; tb.translucent = YES;
效果如下图在边缘的位置们依然能看出视图层之间的层叠,所以查看了些资料,原来正确的方法是子类化UIToolbar,设置其backgroundColor
@interface TranslucentToolbar : UIToolbar @end
@implementation TranslucentToolbar - (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 在需要创建的地方使用子类化的UIToolbar
TranslucentToolbar *tb; tb = [[TranslucentToolbar alloc]initWithFrame:barFrame];
效果如下,得到了一个透明的UIToolbar