У меня есть UICollectionView
и SegmentController
.
Требуемый конечный результат:
SegmentController
в поле зрения, UICollectionView
под ним
Перед добавлением ограничений:
После добавления ограничений (обратите внимание, что SegmentController
почти полностью скрыт):
Добавлены следующие ограничения:
ProductsCollection.removeConstraints (ProductsCollection.constraints) SegmentController.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
SegmentController.centerXAnchor.constraint(equalTo: view.centerXAnchor),
SegmentController.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 100),
//ProductsCollection.topAnchor.constraint(equalTo: SegmentController.bottomAnchor, constant: 10),
ProductsCollection.leftAnchor.constraint(equalTo: view.leftAnchor),
ProductsCollection.rightAnchor.constraint(equalTo: view.rightAnchor),
ProductsCollection.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
Я предполагаю, что проблема в том, что яне добавил ограничений SegmentController, но у меня был тот же результат, когда я добавил:
SegmentController.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 100),
SegmentController.centerXAnchor.constraint(equalTo: view.centerXAnchor),
Редактировать:
Это представление наследуется от другого, которое имеет:
private func createProductsCollection()
{
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 200, height: 250)
self.ProductsCollection = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
ProductsCollection.dataSource = self
ProductsCollection.delegate = self
ProductsCollection.register(ProductsCollectionViewCell.self, forCellWithReuseIdentifier: "product_collection_cell")
ProductsCollection.backgroundColor = UIColor.clear
self.view.addSubview(ProductsCollection)
ProductsCollection.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
NSLayoutConstraint(item: ProductsCollection, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 20),
NSLayoutConstraint(item: ProductsCollection, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -50), //leaving space for search field
NSLayoutConstraint(item: ProductsCollection, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0),
NSLayoutConstraint(item: ProductsCollection, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0)
])
}
РЕДАКТИРОВАТЬ # 2:
Я сейчас использую следующие ограничения:
SegmentController.translatesAutoresizingMaskIntoConstraints = false
SegmentController.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
SegmentController.heightAnchor.constraint(equalToConstant: 40).isActive = true
SegmentController.widthAnchor.constraint(equalToConstant: 120).isActive = true
SegmentController.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
self.view.addSubview(ProductsCollection)
ProductsCollection.translatesAutoresizingMaskIntoConstraints = false
ProductsCollection.topAnchor.constraint(equalTo: self.SegmentController.bottomAnchor).isActive = true
ProductsCollection.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
ProductsCollection.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
ProductsCollection.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
И мой взгляд теперь выглядит так: