Git Product home page Git Product logo

swiftycollectionviewflowlayout's Introduction

SwiftyCollectionViewFlowLayout

A collection view layout capable of laying out views in vertically and horizontal scrolling grids and lists.

Introduction

SwiftyCollectionViewFlowLayout is a UICollectionViewLayout subclass for laying out vertically and horizontal scrolling grids and lists of items. Compared to UICollectionViewFlowLayout, SwiftyCollectionViewFlowLayout supports many additional features:

  • Support Vertical and Horizontal scroll direction.
  • Support water-flow and grid list.
  • Hiding or showing headers and footers on a per-section basis.
  • Self-sizing headers and footers.
  • Headers and footers offset.
  • Headers and footers direction.
  • Pinned (sticky) headers and footers on a per-section basis.
  • Section background that can be hidden/visible on a per-section basis.
  • Section background inset.
  • Per-item self-sizing preferences (self-size and statically-size items anywhere in your collection view).
  • Item width/height based on a fraction of the total available width/height.
  • Item width/height ratio.

Preview

Support Preview
Water-Flow
Tag-List
Auto-Size
PinnedHeaderOrFooter

Getting Start

Requirements

  • Deployment target iOS 11.0+
  • Swift 5+
  • Xcode 14+

Installation

CocoaPods

pod 'SwiftyCollectionViewFlowLayout'

Or

pod 'SwiftyCollectionViewFlowLayout', :git => "https://github.com/liujunliuhong/SwiftyCollectionViewFlowLayout.git"

Usage

Once you've integrated the SwiftyCollectionViewFlowLayout into your project, using it with a collection view is easy.

Setting up cells and headers

SwiftyCollectionViewFlowLayout requires its own UICollectionViewCell and UICollectionReusableView subclasses:

  • SwiftyCollectionViewCell
  • SwiftyCollectionReusableView

These two types enable cells and supplementary views to self-size correctly when using SwiftyCollectionViewFlowLayout .

Importing SwiftyCollectionViewFlowLayout

At the top of the file where you'd like to use SwiftyCollectionViewFlowLayout, import SwiftyCollectionViewFlowLayout.

import SwiftyCollectionViewFlowLayout

Setting up the collection view

Create your UICollectionView instance, passing in a SwiftyCollectionViewFlowLayout instance for the layout parameter.

let layout = SwiftyCollectionViewFlowLayout()
layout.scrollDirection = .vertical

let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self

view.addSubview(collectionView)
// Use SnapKit
collectionView.snp.makeConstraints { make in
    make.edges.equalToSuperview()
}

Registering cells and supplementary views

// Cell
collectionView.register(MyCell.classForCoder(), forCellWithReuseIdentifier: "MyCellReuseIdentifier")

// Header
collectionView.register(MyHeaderView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "MyHeaderReuseIdentifier")

// Footer
collectionView.register(MyFooterView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "MyFooterReuseIdentifier")

Section Background view

SwiftyCollectionViewFlowLayout support section background view. (very like decoration view)

collectionView.register(DecorationView.classForCoder(), forSupplementaryViewOfKind: SwiftyCollectionViewFlowLayout.SectionBackgroundElementKind, withReuseIdentifier: NSStringFromClass(DecorationView.classForCoder()))

section background view do not self-size. so, the section background view can subclasses from UICollectionReusableView

Configuring the delegate

Here's an example delegate implementation (For more usage, please check Demo):

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, itemSizeModeAt indexPath: IndexPath) -> SwiftyCollectionViewLayoutSizeMode {
    return .init(width: .fractionalFull(divisor: 2), height: .absolute(50))
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, visibilityModeForHeaderInSection section: Int) -> SwiftyCollectionViewLayoutSupplementaryVisibilityMode {
    return .visible(sizeMode: .init(width: .full, height: .absolute(75)))
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, visibilityModeForFooterInSection section: Int) -> SwiftyCollectionViewLayoutSupplementaryVisibilityMode {
    return .hidden
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, headerDirection section: Int) -> SwiftyCollectionViewLayoutSupplementaryDirection {
    return .left
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, footerDirection section: Int) -> SwiftyCollectionViewLayoutSupplementaryDirection {
    return .left
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, sectionInsetContainHeader section: Int) -> Bool {
    return false
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, sectionInsetContainFooter section: Int) -> Bool {
    return false
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, sectionType section: Int) -> SwiftyCollectionViewSectionType {
    return .row(direction: .left, alignment: .center)
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, visibilityModeForBackgroundInSection section: Int) -> SwiftyCollectionViewLayoutBackgroundVisibilityMode {
    return .visible
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, backgroundInset section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, headerPinToVisibleBounds section: Int) -> Bool {
    return true
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: SwiftyCollectionViewFlowLayout, footerPinToVisibleBounds section: Int) -> Bool {
    return false
}

You must implement this method: collectionView(_:layout:sectionType:).

Author

liujun, [email protected]

swiftycollectionviewflowlayout's People

Contributors

liujunliuhong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

swiftycollectionviewflowlayout's Issues

Section based scroll direction

Hi, can you add section based scroll direction for example one section has horizontal other vertical? Like apple store ui

Performance slows

Hi, I also tested library with 10k+ test data when I scroll it really slow down scrolling and make lagging, because of the public override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? here, we check every item in every section for intersection. We can use binary search for intersection.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.