如何在使用约束的表格视图单元格之间创build空间?

我的故事板的截图

正如您在屏幕截图中所看到的,顶部图像和屏幕顶部之间没有空间。 而且,我的所有图片的尺寸都不一样。 如何在这些单元格之间创build空间,并使所有图像的大小相同。

你可以在UIImageView上创build一个高度约束。 然后创build一个宽高比约束,并将其设置为类似于16:9的东西。 然后将UIImageViewcontentMode属性设置为AspectFill并打开Clip To Bounds

然后,将UIImageView的顶部限制在单元格的顶部,底部到底部,尾部以15(或其他)的常量结尾。

UILabelUIImageView之间创build一个Center-Y(垂直居中)和一个水平间距约束。

要停止剪辑标签,请在其与单元格之间创build一个尾随约束,并将值设置为> = 15或其他值。 然后将Number of Lines属性设置为2(如果名称在iPhone 4 / 4s / 5上运行时间很长,则将其设置为0可能导致在单元格的顶部和底部出现剪切)。

我build议去收集意见,你可以轻松地pipe理单元格间距:

1)在故事板上添加collection view

2)以类似的方式添加它的DataSourceDelegate到表视图(select集合视图,拖放到黄色图标)。

3)在单元格中添加imageView

4)相应地调整cell大小。

5)也提供单元格间距检查下面的图像。

在这里输入图像说明

6)调整线的最小间距,它将提供你在找什么。

控制器类 – :

 import UIKit //controller class with 3 needed protocols class UnderlineViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { //viewDidLoad override func viewDidLoad() { super.viewDidLoad() } //didReceiveMemoryWarning override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //numberOfItemsInSection func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ return 5 } //dequeueReusableCell func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) return cell } //numberOfSections func numberOfSections(in collectionView: UICollectionView) -> Int{ return 1 } // sizeForItemAt for each row public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{ return CGSize(width: view.frame.width, height: 200) } } 

输出- :

在这里输入图像说明

也取消选中scrollViewInsets

1)点击你的控制器的黄色图标,然后selectAttribute Inspector

2)寻找调整滚动查看插图,并取消选中它。

在这里输入图像说明