美文网首页
在使用Masnory的同时,使用CAShapeLayer和UIB

在使用Masnory的同时,使用CAShapeLayer和UIB

作者: CL0601 | 来源:发表于2020-03-04 11:35 被阅读0次

    今天在使用UIBezierPath给UIImageView设置任意圆角的时候发现图片不展示了,源码是这样的:

    - (UIImageView *)imageV{
        if (!_imageV) {
            _imageV = [[UIImageView alloc]init];
            _imageV.contentMode = UIViewContentModeScaleAspectFill;
            UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:_imageV.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(3, 3)];
            CAShapeLayer *shape = [[CAShapeLayer alloc] init];
            [shape setPath:rounded.CGPath];
            _imageV.layer.mask = shape;
            _imageV.layer.masksToBounds = YES;
            _imageV.backgroundColor = [UIColor pq_whiteColor];
        }
        return _imageV;
    }
    [self.view addSubview:self.imageV];
        [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(15);
            make.width.mas_equalTo(kScreenWidth-30);
            make.height.mas_equalTo(kScreenWidth/2-15);
        }];
    

    查看图层之后发现imageV的高度为0,原因是设置圆角的时候imageV的frame跟bounds都是0,所以在设置完约束条件之后需要这样做:

    [self.imageV layoutIfNeeded];
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:_imageV.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(3, 3)];
          CAShapeLayer *shape = [[CAShapeLayer alloc] init];
            [shape setPath:rounded.CGPath];
            self.imageV.layer.mask = shape;
    

    问题解决!

    相关文章

      网友评论

          本文标题:在使用Masnory的同时,使用CAShapeLayer和UIB

          本文链接:https://www.haomeiwen.com/subject/recmlhtx.html