我有一个UICollectionView
这显示了照片。 我创建使用的CollectionView UICollectionViewFlowLayout
。 它的工作原理很好,但我想有间距的利润。 是否有可能做到这一点使用UICollectionViewFlowLayout
或我要实现自己的UICollectionViewLayout
?
Answer 1:
您可以使用collectionView:layout:insetForSectionAtIndex:
方法为您UICollectionView
或设置sectionInset
的财产UICollectionViewFlowLayout
连接到你的对象UICollectionView
:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(top, left, bottom, right);
}
要么
UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[aFlowLayout setSectionInset:UIEdgeInsetsMake(top, left, bottom, right)];
Answer 2:
设置在Interface Builder插图像显示在下面的屏幕截图
会导致这样的事情:
Answer 3:
要在整个加间距UICollectionView
:
UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout*) collection.collectionViewLayout;
flow.sectionInset = UIEdgeInsetsMake(topMargin, left, bottom, right);
为了与同一行的元素(如果你水平滚动栏),以及它们的大小之间的间距发挥:
flow.itemSize = ...;
flow.minimumInteritemSpacing = ...;
Answer 4:
只是为了纠正这个网页一些错误的信息:
1- minimumInteritemSpacing: 最小间距的同一行中的项之间使用。
默认值为:10.0。
(对于垂直滚动网格,该值表示在同一行中的项目之间的最小间距)。
2- minimumLineSpacing: 最小间距到网格中的项目的线之间使用。
参考: http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewFlowLayout_class/Reference/Reference.html
Answer 5:
斯威夫特4
let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout // If you create collectionView programmatically then just create this flow by UICollectionViewFlowLayout() and init a collectionView by this flow.
let itemSpacing: CGFloat = 3
let itemsInOneLine: CGFloat = 3
flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
let width = UIScreen.main.bounds.size.width - itemSpacing * CGFloat(itemsInOneLine - 1) //collectionView.frame.width is the same as UIScreen.main.bounds.size.width here.
flow.itemSize = CGSize(width: floor(width/itemsInOneLine), height: width/itemsInOneLine)
flow.minimumInteritemSpacing = 3
flow.minimumLineSpacing = 3
Answer 6:
使用setMinimumLineSpacing:
和setMinimumInteritemSpacing:
在UICollectionViewFlowLayout
-object。
Answer 7:
现代斯威夫特,自动布局计算
虽然这个线程已经包含了一些有用的答案,我想补充一个现代版的雨燕 ,根据威廉·胡的答案。 它也提高了两件事情:
- 不同线路之间的间距将现在总是在同一行匹配项之间的间距。
- 通过设置最小宽度,代码自动计算成一排的项目数和风格适用于流布局。
下面的代码:
// Create flow layout
let flow = UICollectionViewFlowLayout()
// Define layout constants
let itemSpacing: CGFloat = 1
let minimumCellWidth: CGFloat = 120
let collectionViewWidth = collectionView!.bounds.size.width
// Calculate other required constants
let itemsInOneLine = CGFloat(Int((collectionViewWidth - CGFloat(Int(collectionViewWidth / minimumCellWidth) - 1) * itemSpacing) / minimumCellWidth))
let width = collectionViewWidth - itemSpacing * (itemsInOneLine - 1)
let cellWidth = floor(width / itemsInOneLine)
let realItemSpacing = itemSpacing + (width / itemsInOneLine - cellWidth) * itemsInOneLine / (itemsInOneLine - 1)
// Apply values
flow.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
flow.itemSize = CGSize(width: cellWidth, height: cellWidth)
flow.minimumInteritemSpacing = realItemSpacing
flow.minimumLineSpacing = realItemSpacing
// Apply flow layout
collectionView?.setCollectionViewLayout(flow, animated: false)
Answer 8:
使用collectionViewFlowLayout.sectionInset
或collectionView:layout:insetForSectionAtIndex:
是正确的。
然而,如果你的CollectionView有多个部分,并要边距添加到整个的CollectionView,我建议使用滚动视图contentInset:
UIEdgeInsets collectionViewInsets = UIEdgeInsetsMake(50.0, 0.0, 30.0, 0.0);
self.collectionView.contentInset = collectionViewInsets;
self.collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(collectionViewInsets.top, 0, collectionViewInsets.bottom, 0);
Answer 9:
为了把间距CollectionItem
的使用这
在写这viewDidLoad中两线
UICollectionViewFlowLayout *collectionViewLayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;
collectionViewLayout.sectionInset = UIEdgeInsetsMake(<CGFloat top>, <CGFloat left>, <CGFloat bottom>, <CGFloat right>)
Answer 10:
设置insetForSectionAt
的财产UICollectionViewFlowLayout
连接到你的对象UICollectionView
请务必加入此协议
UICollectionViewDelegateFlowLayout
迅速
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets (top: top, left: left, bottom: bottom, right: right)
}
Objective - C的
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(top, left, bottom, right);
}
Answer 11:
为了增加利润,以指定的细胞,就可以使用此自定义流布局。 https://github.com/voyages-sncf-technologies/VSCollectionViewCellInsetFlowLayout/
extension ViewController : VSCollectionViewDelegateCellInsetFlowLayout
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForItemAt indexPath: IndexPath) -> UIEdgeInsets {
if indexPath.item == 0 {
return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
}
return UIEdgeInsets.zero
}
}
Answer 12:
在Objective-C
CGFloat spacing = 5;
UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout*)_habbitCollectionV.collectionViewLayout;
flow.sectionInset = UIEdgeInsetsMake(0, spacing, 0, spacing);
CGFloat itemsPerRow = 2;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat oneMore = itemsPerRow + 1;
CGFloat width = screenRect.size.width - spacing * oneMore;
CGFloat height = width / itemsPerRow;
flow.itemSize = CGSizeMake(floor(height), height);
flow.minimumInteritemSpacing = spacing;
flow.minimumLineSpacing = spacing;
你所要做的就是改变itemsPerRow值,它会相应地更新每行项目的数量。 此外,如果您想了解更多或更少的一般间距可以改变间距值。
Answer 13:
在迅速4和的autoLayout,你可以使用sectionInset这样的:
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: (view.frame.width-40)/2, height: (view.frame.width40)/2) // item size
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10) // here you can add space to 4 side of item
collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout) // set layout to item
collectionView?.register(ProductCategoryCell.self, forCellWithReuseIdentifier: cellIdentifier) // registerCell
collectionView?.backgroundColor = .white // background color of UICollectionView
view.addSubview(collectionView!) // add UICollectionView to view
Answer 14:
要添加每个部分之间10px的分离只是写这
flowLayout.sectionInset = UIEdgeInsetsMake(0.0, 0.0,10,0);
Answer 15:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(7, 10, 5, 10);
}