【发布时间】:2019-12-17 11:15:37
【问题描述】:
首先,我不使用情节提要,而是以编程方式在UICollectionView 中设置我的元素。
根据我的 UICollectionView 中是否有项目,我会在距离顶部 16 像素或 88 像素的位置显示 UIView,为此,我使用了似乎不受尊重的 switch 条件。
我使用经典条件:
switch numberOfItems {
case 0:
print("nothing here")
holderButtons.isHidden = true
BottomView.anchor(top: bottomContainerView.topAnchor, leading: nil, bottom: nil, trailing: nil, padding: .init(top: 16, left: 0, bottom: 0, right: 0))
default:
print("1 or more")
holderButtons.isHidden = false
BottomView.anchor(top: bottomContainerView.topAnchor, leading: nil, bottom: nil, trailing: nil, padding: .init(top: 88, left: 0, bottom: 0, right: 0))
}
我也试过if...else。
该条件适用于隐藏视图,但不适用于我的BottomView。
这是来自调试区域的消息:
<NSLayoutConstraint:0x280f385a0 V:|-(16)-[UIView:0x1479337d0] (active, names: '|':UIView:0x147933410 )>",
"<NSLayoutConstraint:0x280f13930 V:|-(88)-[UIView:0x1479337d0] (active, names: '|':UIView:0x147933410 )>
我看不出它有什么问题。 有什么想法吗?
【问题讨论】:
-
你在改变约束值后使用过“yourView.layoutIfNeeded()”吗?
-
numberOfItems是硬编码的还是来自服务器的动态?如果您的collectionView最初有 0 个项目,后来它有更多项目,那么您将遇到此问题。 -
@Fadielse 我从主 ViewController 重新加载数据,它工作正常,期待当我滚动到顶部时(一旦项目数比我的视图长):我的 BottomView 取其初始值(顶部: 16).
-
@GaloTorresSevilla,我的项目作为实体存储在 CoreData 中。
-
当我回到我之前的视图并再次回到我的 CollectionView 时,没有更多的问题,没有更多的错误......
标签: swift layout nslayoutconstraint