如何向UICollectionViewCell添加阴影和拐角半径,包括页眉和页脚

时间:2020-09-02 15:11:44

标签: ios swift uicollectionview uicollectionviewcell

我正在使用iOS中的UICollectionCompositionalLayouts构建应用程序,我想重新创建以下单元格

Collection View Cell with Shadow including Header and Footer

该单元格的所有侧面都有阴影和圆角,包括页眉和页脚,就好像它是该单元格的一部分一样。当我尝试在UICollectionViewCell中重新创建此外观时,只能将Shadow添加到标题中,其他所有内容都将被忽略。以下是我的手机

My Collection View Cell

无论我如何设置组成该部分的单个集合视图单元,阴影都永远不会出现,并且该单元似乎不会像第一个图像那样连接。

这是我用来设置单元格的代码

标题的自定义代码

class HeaderWorkoutOfDay:UICollectionReusableView {


let mainView = UIView()

static let reuseIdentifier = String(describing: self)

static let elementKind = String(describing: self)


let programmeNameButton = UIButton(type: .system)

let workoutNameLabel = UILabel()

override init(frame: CGRect) {
    super.init(frame: frame)
    
    addSubview(mainView)
    
    mainView.snp.makeConstraints { (view) in
        view.top.left.bottom.right.equalToSuperview()
    }
    
    
    mainView.addSubview(programmeNameButton)
    mainView.addSubview(workoutNameLabel)
    costumizeViews()
    setupConstraints()
    
    
    
    
    
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

private func costumizeViews(){
    
    programmeNameButton.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .regular)
    programmeNameButton.contentHorizontalAlignment = .left
    
    workoutNameLabel.font = UIFont.systemFont(ofSize: 25, weight: .bold)
    
    self.mainView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
    
    self.mainView.layer.cornerRadius = 14
    self.mainView.layer.masksToBounds = true
    self.mainView.backgroundColor = UIColor.white
    
    self.layer.shadowOpacity = 1.0
    self.layer.shadowColor = UIColor(red: 200, green: 200, blue: 200).cgColor
    self.layer.shadowRadius = 10
    self.layer.masksToBounds = false
    self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    
    
}

private func setupConstraints(){
    
    programmeNameButton.snp.makeConstraints { (button) in
        button.left.top.right.equalToSuperview().offset(8)
        button.bottom.equalTo(workoutNameLabel.snp.top).offset(-8)
    }
    
    
    workoutNameLabel.snp.makeConstraints { (label) in
        label.left.equalToSuperview().offset(8)
        label.top.equalTo(programmeNameButton.snp.bottom).offset(8)
        label.bottom.right.equalToSuperview().offset(-8)
    }
    
}

}

我对单个单元格和页脚使用的设置相同,也是UICollectionReusableView

如何重新创建第一张图片的外观?

0 个答案:

没有答案