【发布时间】:2015-04-07 19:39:48
【问题描述】:
如何调整集合视图部分之间的间距。
【问题讨论】:
-
你试过
collectionView:layout:insetForSectionAtIndex:吗?
标签: ios iphone uicollectionview collectionview uicollectionreusableview
如何调整集合视图部分之间的间距。
【问题讨论】:
collectionView:layout:insetForSectionAtIndex:吗?
标签: ios iphone uicollectionview collectionview uicollectionreusableview
这是一个布局问题,因此答案将在您用于集合视图的任何布局中。如果您使用UICollectionViewFlowLayout,那么您需要设置sectionInset。例如
self.collectionView.collectionViewLayout.sectionInset = UIEdgeInsetsZero;
【讨论】:
您可以使用该方法来实现:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
//{top, left, bottom, right}
if ([[sectionHeaderStatusArray objectAtIndex:section] boolValue]) {
return UIEdgeInsetsMake(23, 19, 46, 14);
}
return UIEdgeInsetsZero;
}
【讨论】:
可以通过调整集合视图布局的参数来调整标题高度。以下是完美运行的代码。
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
if ([[sectionHeaderArray objectAtIndex:section] boolValue]) {
return UIEdgeInsetsMake(10, 10, 10, 10);
}
return UIEdgeInsetsZero;
}
【讨论】:
试试这个:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
if ([[sectionHeaderArray objectAtIndex:section] boolValue]) {
return UIEdgeInsetsMake(top, left, bottom, right);
}
return UIEdgeInsetsZero;
}
【讨论】:
这是 Swift 4.2 版本。
这使您可以为不同的部分设置各种插入配置。
/// Formats the insets for the various headers and sections.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if section == 0 {
// No insets for header in section 0
return UIEdgeInsets.zero
} else {
// Normal insets for collection
return UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
}
}
【讨论】: