【问题标题】:Swift - set cornerRadius and shadow at the same timeSwift - 同时设置cornerRadius和阴影
【发布时间】:2019-11-03 16:24:58
【问题描述】:

我正在尝试将 cornerRadiusshadow(底部 + 右侧)设置为我的 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),
    ])


}

}

【问题讨论】:

    标签: ios swift uiimage uicollectionviewcell


    【解决方案1】:

    只需在addShadow()函数中添加一行代码:

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

    然后将圆角半径添加到您的单元格:

    contentview.clipsToBounds = true
    

    【讨论】:

      【解决方案2】:

      问题就在那个时候

      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
         }
      }
      

      【讨论】:

      • 我必须在哪里实现该 sn-p?对不起,不是很有经验:)
      • MainWishlistCell类内
      • 这对我没有任何作用
      猜你喜欢
      • 1970-01-01
      • 2020-06-10
      • 2011-04-11
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      相关资源
      最近更新 更多