我有一个显示得很好的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),
])
}
collectionView.translatesAutoresizingMaskIntoConstraints = false