UIView图层圆角和-drawRect:方法

7

在同时设置UIView层的圆角和重写-drawRect:方法时,是否可能?目前,-drawRect:调用似乎会覆盖层的圆角并使其再次呈现为角形,即使-drawRect:只包含对超类的-drawRect:调用。

2个回答

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;
}

一样的情况。self.opaque = NO 没有起作用,但是 masksToBounds = YES 起了作用。 - mahboudz

3
将opaque属性设置为NO,即可恢复圆角。
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.layer.cornerRadius = KCORNERRAD;
        self.opaque = NO;

    }
    return self;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接