【问题标题】:Shadow not visible for UIImageView with corner带角的 UIImageView 的阴影不可见
【发布时间】:2017-08-22 13:53:49
【问题描述】:

我试图同时给图像添加角落和阴影。如果我删除但我不能显示阴影。我删除了masksToBonds,但后来我丢了角。如何解决?

_iconView.image = [UIImage imageNamed:cellObject.imageName];

_iconView.layer.cornerRadius = 30.0f;
_iconView.clipsToBounds = YES;

_iconView.layer.shadowRadius = 3.0f;
_iconView.layer.shadowColor = [UIColor blackColor].CGColor;
_iconView.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
_iconView.layer.shadowOpacity = 0.5f;
_iconView.layer.masksToBounds = NO;

【问题讨论】:

标签: ios objective-c uiimageview uiimage


【解决方案1】:

你可以设置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 设置图像(并且不让它溢出),然后将其嵌套在支架内,在支架上设置阴影并剪辑 imageView:

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];

【讨论】:

    猜你喜欢
    • 2020-09-12
    • 2017-05-19
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 2023-04-01
    相关资源
    最近更新 更多