我正在尝试在cornerRadius
内设置UIImageVIew
和UICollectionViewCell
的阴影。问题是我设置clipToBounds = true
并停止阴影和cornerRadius
。
我想拥有cornerRadius,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 :(得分:1)
如果你在imageview上给角半径和阴影,则不起作用。尝试在视图中使用imageview,然后在视图上给出角半径,在imageview上给出阴影
答案 1 :(得分:0)
答案 2 :(得分: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)
}