在故事板中的集合视图末尾添加按钮

时间:2015-07-23 04:07:48

标签: ios swift uicollectionview uicollectionviewcell

我在故事板中有一个UICollectionViewController。我知道如何添加单元格并修改它们但由于某些原因我无法在UICollectionView之后添加任何其他视图或UI元素。

有没有办法在故事板中这样做?如果不能如何以编程方式执行此操作?

2 个答案:

答案 0 :(得分:5)

在故事板中,您可以通过为您的UICollectionView选择单选按钮标题" Section Footer",然后在那里拖动UIButton来启用它。您也可以覆盖此功能:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

如果您是UICollectionViewFlowLayout

,您可能还需要设置页脚的参考尺寸

答案 1 :(得分:1)

Swift 2.1解决方案:

在故事板中选择Collection View > Attributes Inspector > Enabled Section Footer

启用后,会显示剖面视图,您可以将视图拖动到该视图。

Select the header view, and set the Identifier。例如:FooterViewID

接下来,在相关的视图控制器文件中,写:

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
    let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "FooterViewID", forIndexPath: indexPath)
    return footerView
}

页脚现在应该出现在你的界面底部。

相关问题