我遇到与此处链接相同的问题: Can't add a corner radius and a shadow
如果我把maskToBounds = YES,我得到圆角,但没有阴影 如果我把maskToBounds = NO,我得到阴影,但没有圆角。
然后我按照上面那个链接中的指令设置maskToBounds = NO,“而是设置角半径并用圆角矩形设置阴影的bezier路径。保持两者的半径相同”。但是,我没有圆角,也没有任何阴影! (即没有阴影的方形图像)你可以帮我解决这个问题吗?我不知道我做错了什么。提前致谢。
self.userImageView.backgroundColor = [UIColor redColor];
self.userImageView.clipsToBounds = NO;
self.userImageView.contentMode = UIViewContentModeCenter;
self.userImageView.layer.masksToBounds = NO;
self.userImageView.layer.borderWidth = 1;
self.userImageView.layer.borderColor = [[UIColor grayColor] CGColor];
self.userImageView.layer.shadowOpacity = 1;
self.userImageView.layer.shadowColor = [[UIColor blackColor] CGColor];
self.userImageView.layer.shadowRadius = 8.0f;
self.userImageView.layer.shadowOffset = CGSizeMake(-3, 0);
self.userImageView.layer.shouldRasterize = YES;
self.userImageView.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:[self.userImageView bounds] cornerRadius:10.0f] CGPath];
[self addSubview:self.userImageView];
答案 0 :(得分:20)
不幸的是,我不认为UIImageView同时支持圆角和阴影。
但是,您可以在UIImageView的超级视图中制作阴影。
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];