UIView layer rounded corners and -drawRect:

拈花ヽ惹草 提交于 2020-01-03 08:48:11

问题


Is it possible to set rounded corners on an UIView's layer and override -drawRect: at the same time? Currently the -drawRect: call seems to override the layer's rounded corners and make them appear angular again, even if the -drawRect: just contains a call to the super's -drawRect:.


回答1:


self.opaque = NO didn't work for me. Setting self.layer.masksToBounds = YES did work, however (tested on iOS 4.3):

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



回答2:


Set the opaque property to NO. You will get your rounded corners back.

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

    }
    return self;
}


来源:https://stackoverflow.com/questions/7078911/uiview-layer-rounded-corners-and-drawrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!