在普通UIView下约束集合视图

问题描述 投票:0回答:1

我有一个显示得很好的UIView。我试图有一个collectionView在它下面显示。当单独调用两个元素时,它们都显示良好,但是当我同时调用这两个元素时,都会遇到约束错误。

我认为我确实加载了:

    configureStatsView()
    configureCollectionView()

ConfigureStatsView:

    private func configureStatsView() {

    view.addSubview(statsView)
    statsView.translatesAutoresizingMaskIntoConstraints = false
    statsView.backgroundColor = .systemRed

    let padding: CGFloat    = 20

    NSLayoutConstraint.activate([

        statsView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        statsView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: padding),
        statsView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -padding),
        statsView.heightAnchor.constraint(equalToConstant: 180),

    ])
}

ConfigureCollectionView:

    private func configureCollectionView() {

    collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: UIHelper.createThreeColumnFlowLayout(in: view))
    view.addSubview(collectionView)
    collectionView.delegate = self
    collectionView.backgroundColor = .systemBackground
    collectionView.register(CustomCell.self, forCellWithReuseIdentifier: CustomCell.resuseID)

    let padding: CGFloat    = 20

    NSLayoutConstraint.activate([

        collectionView.topAnchor.constraint(equalTo: statsView.bottomAnchor, constant: padding),
        collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: padding),
        collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -padding),
        collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),

    ])
}
swift swift3 swift2
1个回答
0
投票
collectionView.translatesAutoresizingMaskIntoConstraints = false
© www.soinside.com 2019 - 2024. All rights reserved.