2017-08-22 85 views
1

我想同時給一個圖像的角落和陰影。如果我刪除,但我不能顯示陰影。我刪除了masksToBonds,但後來我失去了角落。如何解決這個問題?陰影不可見的UIImageView與角落

_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; 
+0

檢查此https://stackoverflow.com/questions/40887532/how-to-give-an-imageview-in-swift3-0-1-shadow-at-the-same-time-with-rounded-corn –

+0

這是迅速的答案。 – birdcage

+0

[UIView可能有重複的圓角和陰影?](https://stackoverflow.com/questions/4754392/uiview-with-rounded-corners-and-drop-shadow) – DonMag

回答

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