添加的约束:
productsCollection.removeConstraints(productsCollection.Constraints)
segmentcontroller.translatesAutoResizingMaskinToConstraints=false
nslayoutConstraint.activate([
segmentcontroller.centerXanchor.constraint(等于:view.centerXanchor)
SegmentController.TopAnchor.Constraint(等于:self.view.safearelayoutguide.TopAnchor,常量:100),
//productsCollection.TopAnchor.Constraint(EqualTo:SegmentController.BottomAnchor,常量:10),
productsCollection.LeftAnchor.Constraint(等于:View.LeftAnchor),
productsCollection.RightAnchor.Constraint(等于:view.RightAnchor),
productsCollection.BottomAnchor.Constraint(等于:view.BottomAnchor)
)
< /代码>
我猜问题是我没有添加SegmentController约束,但添加时得到了相同的结果:
SegmentController.TopAnchor.Constraint(等于:self.view.safearelayoutGuide.TopAnchor,常量:100),
segmentcontroller.centerXanchor.constraint(等于:view.centerXanchor)
< /代码>
<>编辑:
此视图继承自另一个视图,该视图具有:
private func createproductsCollection())
{
let-layout:uiCollectionViewFlowLayout=uiCollectionViewFlowLayout()。
layout.itemsize=cgsize(宽200高250)
self.productsCollection=uiCollectionView(框架:self.view.frame,集合视图布局:布局)
productsCollection.datasource=自己
productsCollection.delegate=自己
productsCollection.register(productsCollectionViewCell.self,forcellWithReuseIdentifier:“product_collection_cell”)。
productsCollection.backgroundcolor=uicolor.clear
self.view.addSubView(产品集合)
productsCollection.translatesAutoResizingMaskinToConstraints=false
nslayoutConstraint.activate([
nslayoutConstraint(项:ProductsCollection,属性:.Top,RelatedBy:.Equal,ToItem:Self.View,属性:.Top,乘数:1,常量:20),
nslayoutConstraint(item:productsCollection,attribute:.bottom,relatedby:.equal,toitem:self.view,attribute:.bottom,multipler:1,constant:-50),//为搜索字段保留空间
nslayoutConstraint(项:ProductsCollection,属性:.Leading,RelatedBy:.Equal,ToItem:Self.View,属性:.Leading,乘数:1,常量:0),
nslayoutConstraint(项:ProductsCollection,属性:.Trailing,RelatedBy:.Equal,ToItem:Self.View,属性:.Trailing,乘数:1,常量:0)
)
}
< /代码>
<编辑> 2:
我现在使用这些约束:
SegmentController.TranslatesAutoResizingMaskinToConstraints=false
segmentcontroller.topanchor.constraint(等于:self.view.safearelayoutguide.topanchor).isactive=true
SegmentController.HeightAnchor.Constraint(EqualToConstant:40).IsActive=true
segmentcontroller.widthanchor.constraint(equalToconstant:120).isactive=true
segmentcontroller.centerXanchor.constraint(等于:self.view.centerXanchor).isactive=true
self.view.addSubView(产品集合)
productsCollection.translatesAutoResizingMaskinToConstraints=false
productsCollection.TopAnchor.Constraint(等于:self.SegmentController.BottomAnchor).IsActive=true
productsCollection.trailinganchor.constraint(等于:self.view.trailinganchor).isactive=true
productsCollection.BottomAnchor.Constraint(等于:self.view.BottomAnchor).IsActive=true
productsCollection.LeadingAnchor.Constraint(等于:self.view.LeadingAnchor).IsActive=true
< /代码>
现在我的观点是:
.ui集合视图
在它下面
添加约束之前:

添加约束后(请注意分段控制器几乎完全隐藏):

添加的约束:
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
现在我的观点是:
