Add Round Corner to UIImageVIew and Display Shadow effect

前端 未结 1 1047
轻奢々
轻奢々 2020-12-31 23:24

I have the same problem as the link here: Can't add a corner radius and a shadow

if I put maskToBounds = YES, I get round corners, but no shadow If I put maskToB

相关标签:
1条回答
  • 2021-01-01 00:09

    Unfortunately, I don't think UIImageView supports rounded corner and shadow at the same time.

    You can, however, make the shadow in the UIImageView's super view.

    CGFloat cornerRadius = 3.0
    
    UIView *container = [[UIView alloc] initWithFrame:aRect];
    container.layer.shadowOffset = CGSizeMake(0, 0);
    container.layer.shadowOpacity = 0.8;
    container.layer.shadowRadius = 5.0;
    container.layer.shadowColor = [UIColor redColor].CGColor;
    container.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:container.bounds cornerRadius:cornerRadius] CGPath];
    
    self.userImageView.layer.cornerRadius = cornerRadius;
    self.userImageView.layer.masksToBounds = YES;
    self.userImageView.frame = container.bounds;
    [container addSubview:self.userImageView];
    [self addSubview:container];
    
    0 讨论(0)
提交回复
热议问题