我试图同时为图像添加角落和阴影。如果我删除但我不能显示阴影。我删除了styles: []
,但后来我失去了角落。如何解决?
masksToBonds
答案 0 :(得分:0)
您可以设置 shadowPath属性来跟踪iconView路径的内容,并设置其角半径,如下所示:
UIImageView * iv = [UIImageView new];
iv.frame = CGRectMake(0, 0, 100, 50);
iv.backgroundColor = [UIColor redColor];
iv.layer.cornerRadius = 10.0f;
iv.image = [UIImage imageNamed:@"settings-120.png"];
iv.layer.cornerRadius = 10.0f;
iv.layer.shadowColor = [UIColor blackColor].CGColor;
iv.layer.shadowOffset = CGSizeZero;
iv.layer.shadowRadius = 3.0f;
iv.layer.shadowOpacity = 1.0f;
iv.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:iv.bounds cornerRadius:iv.layer.cornerRadius].CGPath;
[self.view addSubview:iv];
如果你想用 UIViewContentModeScaleAspectFill 设置一个图像(并没有溢出),那么将它嵌入一个支架,在支架上设置阴影并剪切图像:
UIView * holder = [UIView new];
holder.frame = CGRectMake(0, 0, 100, 50);
holder.layer.cornerRadius = 10.0f;
holder.layer.shadowColor = [UIColor whiteColor].CGColor;
holder.layer.shadowOffset = CGSizeZero;
holder.layer.shadowRadius = 3.0f;
holder.layer.shadowOpacity = 1.0f;
holder.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:holder.bounds cornerRadius:holder.layer.cornerRadius].CGPath;
[self.view addSubview:holder];
UIImageView * iv = [UIImageView new];
iv.frame = holder.bounds;
iv.layer.cornerRadius = holder.layer.cornerRadius;
iv.image = [UIImage imageNamed:@"settings-120.png"];
iv.contentMode = UIViewContentModeScaleAspectFill;
iv.clipsToBounds = true;
[holder addSubview:iv];