Swift - установить одновременно уголRadius и тень - PullRequest
1 голос
/ 03 ноября 2019

Я пытаюсь установить cornerRadius и shadow (внизу + справа) для моего UICollectionViewCell, но мой код не работает. Ни cornerRadius, ни тень не появляются ..

Вот мой код:

class MainWishlistCell: UICollectionViewCell {

let wishlistImage: UIImageView = {
    let v = UIImageView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.image = UIImage(named: "logoGroß")
    return v
}()

let wishlistLabel: UILabel = {
    let v = UILabel()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.text = "Main Wishlist"
    v.font = UIFont(name: "AvenirNext-DemiBold", size: 18)
    v.textColor = .darkGray
    v.textAlignment = .center
    return v
}()

func addShadow() {
    let cornerRadius: CGFloat = 5
    self.wishlistImage.layer.shadowPath = UIBezierPath(roundedRect: self.wishlistImage.bounds, cornerRadius: cornerRadius).cgPath
    self.wishlistImage.layer.shadowRadius = cornerRadius
    self.wishlistImage.layer.shadowOffset = .zero
    self.wishlistImage.layer.shadowOpacity = 0.3
    self.wishlistImage.layer.shadowRadius = 10
    self.wishlistImage.layer.cornerRadius = cornerRadius
    self.wishlistImage.layer.shadowColor = UIColor.black.cgColor
}

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    commonInit()
}



func commonInit() -> Void {
    addShadow()
    contentView.addSubview(wishlistImage)
    contentView.addSubview(wishlistLabel)
    // constrain view to all 4 sides
    NSLayoutConstraint.activate([
        wishlistImage.topAnchor.constraint(equalTo: contentView.topAnchor),
        wishlistImage.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
        wishlistImage.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
        wishlistImage.heightAnchor.constraint(equalToConstant:150),

        wishlistLabel.topAnchor.constraint(equalTo: wishlistImage.bottomAnchor,constant: 1),
        wishlistLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
        wishlistLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
        wishlistLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
    ])


}

}

Ответы [ 2 ]

1 голос
/ 03 ноября 2019

Проблема в этой точке

addShadow()
contentView.addSubview(wishlistImage)
contentView.addSubview(wishlistLabel)

Изображение bounds равно нулю

self.wishlistImage.layer.shadowPath = UIBezierPath(roundedRect: 
             self.wishlistImage.bounds, 
             cornerRadius: cornerRadius).cgPath

Так что вам нужно

var once = true 
override func layoutSubviews() {
   super.layoutSubviews()
   if once {
      addShadow()
      once = false
   }
}
0 голосов
/ 15 ноября 2019

Просто добавьте одну строку кода в функцию addShadow():

layer.masksToBounds = false ---> which set the shadow of the view's layer.

Затем добавьте угловой радиус в вашу ячейку:

contentview.clipsToBounds = true
...