【发布时间】:2018-01-22 22:16:52
【问题描述】:
我正在尝试做一些相当简单的事情,但我不断收到错误。
我有一个包含多个部分的 UITableView。每个部分都有一个标题和一个 UITableViewCell。每个单元格中都有一个 UICollectionView。所有这些视图都必须以编程方式创建(我不能使用情节提要解决方案)。
问题是当我尝试调整 UICollectionView 的大小和位置时。我希望 UICollectionView 填充整个 UITableViewCell,所以起初我尝试使用 self.frame 在 UITableViewCell 的 init 方法中调整 UICollectionView 的大小,如下所示:
var collectionView: UICollectionView? 让collectionViewLayout:UICollectionViewFlowLayout! 覆盖初始化(样式:UITableViewCellStyle,reuseIdentifier:字符串?){ self.collectionViewLayout = UICollectionViewFlowLayout() self.collectionViewLayout.sectionInset = UIEdgeInsets(上:1.0,左:1.0,下:1.0,右:1.0) self.collectionViewLayout.scrollDirection = .vertical super.init(风格:风格,reuseIdentifier:reuseIdentifier) 让 frame = CGRect(x: 0.0, y: 0.0, width: self.frame.width, height: self.frame.height) self.collectionView = UICollectionView(frame: frame, collectionViewLayout: self.collectionViewLayout) self.collectionView?.dataSource = self self.collectionView?.delegate = self }但是,我认为此时没有设置 UITableViewCell 的框架(init 方法调用),因此 UICollectionView 的大小不正确。那是对的吗?
接下来,我尝试使用布局锚点,如下所示:
collectionView?.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
这给了我一个布局约束错误,并说“UICollectionViewProject[63851:2306772] [LayoutConstraints] 无法同时满足约束。”我不明白这怎么可能。我只添加了一个约束,情节提要中什么都没有,它可能会破坏哪些约束?
不知何故,我需要使这个 UICollectionView 与 UITableViewCell 大小相同,并完美地放入其中。
【问题讨论】:
标签: ios swift uitableview uicollectionview