Swift 如何设置集合视图';通过自动布局调整单元格大小
嗨,我正试图通过Swift 如何设置集合视图';通过自动布局调整单元格大小,swift,uicollectionview,uicollectionviewcell,ios-autolayout,Swift,Uicollectionview,Uicollectionviewcell,Ios Autolayout,嗨,我正试图通过自动布局调整单元格大小 我想按3乘3显示单元格 第一个单元格的左边距=0 最后一个单元格的右边距=0 所有细胞的空间都有1pt。就像一个instagram 我应该设置为单元格的大小吗?我想通过自动布局设置约束 我还尝试使用代码设置单元格的大小 这是我的密码: func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, siz
自动布局调整单元格大小
我想按3乘3显示单元格
第一个单元格的左边距=0
最后一个单元格的右边距=0
所有细胞的空间都有1pt。就像一个instagram
我应该设置为单元格的大小吗?我想通过自动布局设置约束
我还尝试使用代码设置单元格的大小
这是我的密码:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(123, 123);
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1;
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1;
}
但是,我认为计算细胞的大小并不准确
如何根据屏幕大小设置单元格大小
我必须在两个单元格之间设置1pt的间距
是否只使用故事板设置
如果没有,如何根据代码进行设置
谢谢。我不相信您可以仅使用故事板来设置大小,因为您无法对运行时动态创建的集合视图单元格等重复项目设置约束
您可以根据提供的信息轻松计算大小。在collectionView(uquo:layout:sizeForItemAt:)
中,您可以访问collectionView
的边界来计算所需的单元格大小:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// Compute the dimension of a cell for an NxN layout with space S between
// cells. Take the collection view's width, subtract (N-1)*S points for
// the spaces between the cells, and then divide by N to find the final
// dimension for the cell's width and height.
let cellsAcross: CGFloat = 3
let spaceBetweenCells: CGFloat = 1
let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
return CGSize(width: dim, height: dim)
}
确保您的类采用协议UICollectionViewDelegateFlowLayout
这适用于所有尺寸的iPhone和iPad。这里有另一个解决方案,但它只适用于iPhone 6
我将尝试更新iphone5/6plus
特别感谢@vacawama
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
// Compute the dimension of a cell for an NxN layout with space S between
// cells. Take the collection view's width, subtract (N-1)*S points for
// the spaces between the cells, and then divide by N to find the final
// dimension for the cell's width and height.
let cellsAcross: CGFloat = 3
let spaceBetweenCells: CGFloat = 1
let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
print(indexPath.row)
var width = dim
//last cell's width
if( (indexPath.row + 1) % 3 == 0){
width = 124
}else {
width = 124.5
}
print(width)
return CGSizeMake(width, dim)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1;
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1;
}
我使用CollectionView的委托方法设置它。这将为您提供2xN设置,但您可以轻松地将其改为3xN。这是一个截图,你可以参考我在GitHub上的项目
基于vacawama答案的Swift 3版本代码:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellsAcross: CGFloat = 3
let spaceBetweenCells: CGFloat = 1
let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
return CGSize(width: dim, height: dim)
}
瓦卡瓦马的回答很好,但我有两个问题。我想自动使用我在故事板中定义的间距
其次,我无法调用这个函数,没有人提到如何调用?为了调用collectionView的sizeForItemAt,您需要扩展UICollectionViewDelegateFlowLayout,而不是扩展UICollectionViewDelegate。我希望这能为其他人节省一些时间
extension MyViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellsAcross: CGFloat = 3
var widthRemainingForCellContent = collectionView.bounds.width
if let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout {
let borderSize: CGFloat = flowLayout.sectionInset.left + flowLayout.sectionInset.right
widthRemainingForCellContent -= borderSize + ((cellsAcross - 1) * flowLayout.minimumInteritemSpacing)
}
let cellWidth = widthRemainingForCellContent / cellsAcross
return CGSize(width: cellWidth, height: cellWidth)
}
}
集合视图单元格现在不支持自动调整大小吗?我不知道@ThuggishNuggets。您必须以某种方式提供与我在这里处理的信息相同的信息,例如跨单元格的数量和单元格之间所需的间距。如果你知道另一种或更好的方法,请添加一个答案。非常感谢~!我只想把差距拉大一点,让差别不那么明显。@vacawama我理解,谢谢你的帮助。我真的很感激你:)谢谢你!它工作得很好。