布局约束无法正常工作

时间:2018-02-19 01:45:15

标签: ios swift uicollectionview nslayoutconstraint

我在获取第二个自定义类时遇到了很多麻烦,这是另一个UICollectionView(绿色背景)限制在上面类的底部。我已经尝试了所有不同类型的约束和常量“0”等。但是它一直将它放在另一个类的顶部。有没有人对正在发生的事情有任何想法?

以下是屏幕截图:enter image description here

class JobCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout   {

private let cellId = "jobCellId"

override init(frame: CGRect) {
    super.init(frame: frame)
    setupViews()

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

let jobLabel: UILabel = {
    let label = UILabel()
    label.backgroundColor = .green
    label.text = "I WANT TO"
    label.textColor = UIColor.rgb(red: 149, green: 149, blue: 149)
    label.font = UIFont(name: "HelveticaNeue-Medium", size: 16)
    label.textAlignment = .left
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

let jobsCollectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal

    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor.red
    collectionView.translatesAutoresizingMaskIntoConstraints = false

    return collectionView
}()

func setupViews() {

    backgroundColor = UIColor.clear

    jobsCollectionView.dataSource = self
    jobsCollectionView.delegate = self
    jobsCollectionView.register(JobSingleCell.self, forCellWithReuseIdentifier: cellId)


    addSubview(jobLabel)
    addSubview(jobsCollectionView)

    jobLabel.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
    jobLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 20).isActive = true
    jobLabel.widthAnchor.constraint(equalToConstant: frame.width).isActive = true
    jobLabel.heightAnchor.constraint(equalToConstant: 35).isActive = true



    jobsCollectionView.topAnchor.constraint(equalTo: jobLabel.bottomAnchor, constant: 0).isActive = true
    jobsCollectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
    jobsCollectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
    jobsCollectionView.heightAnchor.constraint(equalToConstant: frame.height).isActive = true

}

这是第二个蓝色单元格:

class DayCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout  {

private let dayCellId = "dayCellId"

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

    setupViews()

}

let daysCollectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor.red
    collectionView.translatesAutoresizingMaskIntoConstraints = false

    return collectionView
}()

let dayLabel: UILabel = {
    let label = UILabel()
    label.backgroundColor = .green
    label.text = "WHEN"
    label.textColor = UIColor.rgb(red: 149, green: 149, blue: 149)
    label.font = UIFont(name: "HelveticaNeue-Medium", size: 16)
    label.textAlignment = .left
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

func setupViews() {

    backgroundColor = UIColor.clear


    addSubview(dayLabel)

    dayLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
    dayLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
    dayLabel.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
    dayLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

0 个答案:

没有答案