透明なUItoolbarの作成

1015 ワード

今日は透明な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が得られました