我正在尝试建立一个像iPhone一样的图片库,但是我在使用CollectionView时遇到了问题。标题不显示,我不知道为什么。
override func viewDidLoad() {
super.viewDidLoad()
layout.itemSize = CGSize(width: view.frame.width/3, height: view.frame.width/3)
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.backgroundColor = .white
collectionView.register(ImageCell.self, forCellWithReuseIdentifier: "ImageCell")
collectionView.dataSource = self
collectionView.delegate = self
view.addSubview(titleView)
view.addSubview(collectionView)
view.addSubview(button)
NSLayoutConstraint.activate([
titleView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 16),
titleView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor,constant: 16),
titleView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor,constant: 16),
titleView.bottomAnchor.constraint(equalTo: collectionView.topAnchor, constant: -16),
collectionView.topAnchor.constraint(equalTo: titleView.bottomAnchor, constant: 200),
collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -8),
button.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -8),
button.widthAnchor.constraint(equalToConstant: 50),
button.heightAnchor.constraint(equalToConstant: 50)
])
}
1条答案
按热度按时间dhxwm5r41#
你的约束是相互冲突的。
第一步:设置TitleView的高度。
第二:在标题视图和收藏集视图之间只设置一个绑定。你用不同的选项设置了两次。