【问题标题】:UICollectionView custom layout in SwiftSwift 中的 UICollectionView 自定义布局
【发布时间】:2016-01-07 02:43:40
【问题描述】:

我真的需要帮助...

我需要在 UICollectionView 中使用自定义布局,并且我想要像图像一样的布局:

如何快速获得此布局?

【问题讨论】:

    标签: swift uicollectionview uicollectionviewlayout collectionview


    【解决方案1】:

    您需要滚动视图吗?还是固定的? 因为如果您不滚动,您可以使用UIStackView 并为单元格提供不同的宽度乘数。

    但如果您必须滚动,您可以使用https://github.com/keighl/KTCenterFlowLayout 并使用 UICollectionView 中的委托方法更改单元格的大小

    【讨论】:

      【解决方案2】:

      如果item的大小都一样,可以使用UICollectionViewFlowLayout设置itemSize

      如果项目大小不同,可以使用 UICollectionViewFlowLayout 并实现其collectionView:layout:sizeForItemAtIndexPath: 委托方法以返回所需的大小。

      顶部的栏可能是UICollectionElementKindSectionHeader,或者只是一个更大的单元格。

      【讨论】:

        【解决方案3】:

        对于需要滚动布局的人,可以试试 ScrollableStackView 库。这是github页面:

        https://github.com/gurhub/ScrollableStackView

        这里是一些示例代码:

        斯威夫特

        import ScrollableStackView
        
        var scrollable = ScrollableStackView(frame: view.frame)
        view.addSubview(scrollable)
        
        // add your views with 
        let rectangle = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 55))
        rectangle.backgroundColor = UIColor.blue
        scrollable.stackView.addArrangedSubview(rectangle)
        // ...
        

        Objective-C

        @import ScrollableStackView
        
        ScrollableStackView *scrollable = [[ScrollableStackView alloc] initWithFrame:self.view.frame];
        scrollable.stackView.distribution = UIStackViewDistributionFillProportionally;
        scrollable.stackView.alignment = UIStackViewAlignmentCenter;
        scrollable.stackView.axis = UILayoutConstraintAxisVertical;
        [self.view addSubview:scrollable];
        
        UIView *rectangle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 55)];
        [rectangle setBackgroundColor:[UIColor blueColor]];
        
        // add your views with
        [scrollable.stackView addArrangedSubview:rectangle]; 
        // ...
        

        希望对某人有所帮助。

        【讨论】:

          猜你喜欢
          • 2016-06-28
          • 1970-01-01
          • 2018-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-25
          • 1970-01-01
          相关资源
          最近更新 更多