簡體   English   中英

帶角的UIImageView陰影不可見

[英]Shadow not visible for UIImageView with corner

我正在嘗試同時賦予圖像陰影和陰影。 如果我移除但無法顯示陰影。 我刪除了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;

您可以設置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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM