2017-07-19 141 views
0

我試圖設置一個cornerRadiusUIImageVIew的陰影,它位於UICollectionViewCell的內部。問題是我設置了clipToBounds = true並停止了該陰影和cornerRadiusUIImageView問題上的陰影

我想要角落半徑,clipToBounds和圖像的陰影。不知道我在這裏做錯了什麼。

class PostPhotoCell: UICollectionViewCell { 

@IBOutlet weak var selectedImage: UIImageView! 
@IBOutlet weak var deleteButton: UIButton! 


    override func awakeFromNib() 
    { 
    super.awakeFromNib() 

    selectedImage.layer.cornerRadius = 5.0 
    selectedImage.contentMode = .scaleAspectFill 
    //applying overall shadow to image 
    selectedImage.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).cgColor 
    selectedImage.layer.shadowOffset = CGSize(width: 0, height: 0) 
    selectedImage.layer.shadowOpacity = 1.0 
    selectedImage.layer.shadowRadius = 6.0 
    selectedImage.clipsToBounds = true 
    } 
} 

回答

0

你需要有一個UIView如影容器並添加UIImageView了這一點。以下是您需要使陰影和角半徑與UIImageView一起工作的示例。

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 

    let shadowView = UIView(frame: CGRect(x: 100, y: 100, width: 128, height: 128)) 
    shadowView.clipsToBounds = false 
    shadowView.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).cgColor 
    shadowView.layer.shadowOpacity = 1.0 
    shadowView.layer.shadowOffset = CGSize.zero 
    shadowView.layer.shadowRadius = 6.0 
    shadowView.layer.shadowPath = UIBezierPath(roundedRect: shadowView.bounds, cornerRadius: 6.0).cgPath 
    view.addSubview(shadowView) 

    let imageView = UIImageView() 
    imageView.image = UIImage(named: "YOUR_IMAGE_NAME") 
    imageView.frame = shadowView.bounds 
    imageView.clipsToBounds = true 
    imageView.layer.cornerRadius = 5 
    shadowView.addSubview(imageView) 
} 
+1

我得到的cornerRadius,但沒有影子。我試過'selectedImage.layoutIfNeeded()'和'self.layoutIfNeeded()',但什麼也沒有。只是角落半徑 – Dani

+0

請再次嘗試我的修改答案。這次我設置了'masksToBounds = false' – Lawliet

+0

我試過了。但它不起作用。我得到了陰影,但沒有'cornerRadius'。但是從我讀的內容來看,你不能在同一個圖像視圖上設置cornerRadius和shadow。它比這更復雜 – Dani

0

嘗試: selectedImage.layer.masksToBounds =真

檢查的aspectMode和檢查剪輯子視圖。

enter image description here

+0

試過了,也不行 – Dani

0

如果你是給角落半徑和陰影都在imageview上,是行不通的。嘗試使用內視imageview的,然後給圓角半徑的看法和陰影的ImageView