以编程方式将约束添加到ViewControllerCells

时间:2016-12-07 18:52:41

标签: ios swift constraints nslayoutconstraint

使用CollectionViewCells创建“卡式”菜单,我需要单元格之间具有相同的空间| ------ |在任何屏幕尺寸。

我能够达到这一点:

addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-15-[v0]-15-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": appsCollectionView]))

addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": appsCollectionView]))

我在屏幕两侧将边距设置为15,但每当我在更大的屏幕上加载应用程序时,两者之间的空间也会发生变化。

我试过用:

appsCollectionView.leadingAnchor.constraint(equalTo: trailingAnchor).isActive = true

但是我得到了这个日志:

2016-12-07 16:51:14.902586 AppStoreClone[8843:206635] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x608000283fc0 H:[UICollectionView:0x7f8172010200]-(15)-|   (active, names: '|':AppStoreClone.CategoryCell:0x7f8173309710 )>",
    "<NSLayoutConstraint:0x608000284100 H:[AppStoreClone.CategoryCell:0x7f8173309710]-(0)-[UICollectionView:0x7f8172010200]   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000284100 H:[AppStoreClone.CategoryCell:0x7f8173309710]-(0)-[UICollectionView:0x7f8172010200]   (active)>

1 个答案:

答案 0 :(得分:0)

相关问题