UIView图层圆角和-drawRect:

时间:2011-08-16 13:26:20

标签: objective-c cocoa-touch

是否可以在UIView的图层上设置圆角并同时覆盖-drawRect:?目前,-drawRect:调用似乎会覆盖图层的圆角,并使它们再次显示为角度,即使-drawRect:只包含对超级-drawRect:的调用。

2 个答案:

答案 0 :(得分:15)

self.opaque = NO对我不起作用。然而,设置self.layer.masksToBounds = YES确实有效(在iOS 4.3上测试):

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if( self )
    {
        self.layer.cornerRadius = 6.0f;
        self.layer.masksToBounds = YES;
    }
    return self;
}

答案 1 :(得分:3)

将opaque属性设置为NO。你会得到圆角。

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.layer.cornerRadius = KCORNERRAD;
        self.opaque = NO;

    }
    return self;
}