• Stars
    star
    243
  • Rank 166,489 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 6 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

UICollectionViewSplitLayout makes collection view more responsive.

uicollectionviewsplitlayout_logo

Platform Swift 4.2 License Version Carthage compatible

UICollectionViewSplitLayout makes collection view more responsive.

oct-30-2018 19-53-04

What's this?

UICollectionViewSplitLayout is a subclass of UICollectionViewLayout. It can divide sections into two columns.

Collection view has "Section" which organizes item collection. UICollectionViewFlowLayout places them from top to bottom.

On the other hands, UICollectionViewSplitLayout divides sections into two columns. You can dynamically update the width of them and which column each section is on.

For example, UICollectionViewSplitLayout can change the number of column according to device orientation. All you need is assigning value to leftSideRatio when changing screen size. This figure describes that a collection view has three sections (red, blue and green) and UICollectionViewSplitLayout aligns them side by side.

rendering_well

UICollectionViewSplitLayout looks like UIStackView. But it has a more memory efficient architecture. So you can align a lot of cells with it.

It may be hard to imagine how it works, please run EmojiCollectionViewController.

Requirement

  • iOS 9.0+
  • Swift 4.2

Installation

Carthage

1. create Cartfile

github "https://github.com/yahoojapan/UICollectionViewSplitLayout"

2. install

> carthage update

CocoaPods

1. create Podfile

platform :ios, '8.0'
use_frameworks!

pod "UICollectionViewSplitLayout", :git => 'https://github.com/yahoojapan/UICollectionViewSplitLayout.git'

2. install

> pod install

Getting Started

It’s good to start from replacing UICollectionViewFlowLayout with UICollectionViewSplitLayout.

1. Create UICollectionViewController

Set UICollectionViewController on Storyboard.

2018-10-24 10 28 36

Add implementation to construct a collection view.

import UIKit

private let reuseIdentifier = "Cell"

class BasicCollectionViewController: UICollectionViewController {
    
    var dataSource: [[UIColor]] = [
        (0..<20).map { _ in .red },
        (0..<20).map {  _ in .blue },
        (0..<20).map {  _ in .green }
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return dataSource.count
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource[section].count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
        cell.backgroundColor = dataSource[indexPath.section][indexPath.row]
        return cell
    }
}

Build the code.

2018-10-24 10 27 42

It shows three sections whose items have different colors.

2. Input "UICollectionViewSplitLayout" as Custom Layout Class

Switch "Flow" to "Custom" in Layout attribute and input "UICollectionViewSplitLayout" into Class and Module attributes.

2018-10-30 11 07 59

3. Assign parameters to UICollectionViewSplitLayout object

Connect the layout class to source code. Assign the parameters on viewDidLoad()

    @IBOutlet weak var layout: UICollectionViewSplitLayout!
 
    override func viewDidLoad() {
        super.viewDidLoad()
        //...
        //...
        
        // margins for each section
        layout.minimumItemLineSpacing = 8
        layout.minimumInterItemSpacing = 8
        layout.sectionInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
        
        // Column Setting
        layout.leftSideRatio = 0.4
        layout.splitSpacing = 8
       
        //...
        //...
    }

4. Implement the layout delegate for UICollectionViewSplitLayout

Implement UICollectionViewDelegateTwoColumnLayout. The following methods is required.

extension BasicCollectionViewController: UICollectionViewDelegateSectionSplitLayout {
    // Fix the size of each item as UICollectionViewDelegateFlowLayout does. calculateFixedWidthRaughly() is utility method UICollectionViewSplitLayout has.
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath, width: CGFloat, side: UICollectionViewSplitLayoutSide) -> CGSize {
        let width = layout.calculateFixedWidthRaughly(
                               to: 3, 
                               of: side, 
                               minimumInterItemSpacing: layout.minimumInterItemSpacing, 
                               sectionInset: layout.sectionInset)
        return CGSize(width: width, height: width)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sideForSection section: Int) -> UICollectionViewSplitLayoutSide {
        // when a section number is odd, the items are placed into left side. 
        return section % 2 == 0 ? .left : .right
    }
}

It arranges the sections side by side.

2018-10-30 23 41 03

See BasicCollectionViewController to run the above example.

Architecture

It is one of the UICollectionViewLayout. So you can change a layout without updating code in UICollectionViewDelegate and UICollectionViewDataSource. It is reasonable to apply a new layout. All you have to do is studying layout class and the delegate.

Usage

How to Split

UICollectionViewSplitLayout calculates width of the left and right side with the following parameter.

leftSideRatio = 0.4

It has a Float value which is the ration of left side to the entire width. In addition, left and right sides have spacing between them.

splitSpacing = 10

If a collection view has 110pt width, the above setting requires 40pt width to the left side and 60pt width to the right side. When leftSideRatio has 0.0, splitSpacing is ignored as an exception.

You can choose which side each section is in. UICollectionViewDeleagteSectionSplitLayout provides a method to do that.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sideForSection section: Int) -> UICollectionViewSplitLayoutSide

UICollectionViewSplitLayout hooks the delegate every time a collection view calls invalidateLayout().

Margins

UICollectionViewSplitLayout has these parameters to determine margins.

/// The minimum spacing to use between items in the same row.
open var minimumInterItemSpacing: CGFloat
/// The minimum spacing to use between lines of items in the grid.
open var minimumItemLineSpacing: CGFloat
/// The margins used to lay out content in a section
open var sectionInset: UIEdgeInsets

They have the corresponding delegate methods by section (optional).

// section inset to each section
@objc optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int, side: UICollectionViewSplitLayoutSide) -> UIEdgeInsets
    
// minimumInterItemSpacing to each section
@objc optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInterItemSpacingForSectionAtIndex section: Int, side: UICollectionViewSplitLayoutSide) -> CGFloat

// minimumItemLineSpacing to each section
@objc optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumItemLineSpacingForSectionAtIndex section: Int, side: UICollectionViewSplitLayoutSide) -> CGFloat

Pinning (iOS 11~)

UICollectionViewSplitLayout pins seciton header like UICollectionViewFlowLayout. This feature is supported from iOS 11. sectionPinToVisibleBounds enables to work it.

Setting Attriutes

UICollectionViewSplitLayout uses three kinds of attribute, item, supplementary view and decoration view.

Item

You need to implement cell sizing. UICollectionViewSplitLayout provides a delegate to implement it.

func collectionView(
    _ collectionView: UICollectionView, 
    layout collectionViewLayout: UICollectionViewLayout, 
    sizeForItemAtIndexPath indexPath: IndexPath, 
    width: CGFloat, 
    side: UICollectionViewSplitLayoutSide) -> CGSize

You can divide the sizes evenly with a utility method.

open func calculateFixedWidthRaughly(
    to num: Int, 
    of side: UICollectionViewSplitLayoutSide, 
    minimumInterItemSpacing: CGFloat, 
    sectionInset: UIEdgeInsets) -> CGFloat

Supplementary view for header and footer

You can implement header and footer sizing. UICollectionViewSplitLayout provides delegate to implement it. If the sizes are zero, header and footer are ignoured.

// header
@objc optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int, width: CGFloat, side: UICollectionViewSplitLayoutSide) -> CGSize

// footer size
@objc optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int, width: CGFloat, side: UICollectionViewSplitLayoutSide) -> CGSize

decoration view for backgroundColor

UICollectionSplitLayout has a special decoration view. It has the same size as the section. You can determine backgroundColor to each section with the following delegate.

@objc optional func collectionView(
    _ collectionView: UICollectionView, 
    layout collectionViewLayout: UICollectionViewLayout,
    itemsBackgroundColorFor section: Int, 
    side: UICollectionViewSplitLayoutSide) -> UIColor?

Line Height Normalization

If isNormalizingLineHeight is true, item height is adjusted to outline height of the line (defalut: false).

open var isNormalizingLineHeight: Bool

License

The MIT License (MIT)

Copyright (c) 2018 Yahoo Japan Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

NGT

Nearest Neighbor Search with Neighborhood Graph and Tree for High-dimensional Data
C++
1,253
star
2

objc2swift

Open Source Obj-C to Swift Converter.
Scala
1,033
star
3

SwiftyXMLParser

Simple XML Parser implemented in Swift
Swift
575
star
4

JGLUE

JGLUE: Japanese General Language Understanding Evaluation
Python
304
star
5

AnnexML

AnnexML is a multi-label classifier designed for extremely large label space.
C++
106
star
6

yskip

Incremental Skip-gram Model with Negative Sampling
Shell
69
star
7

yosegi

Yosegi is a Schema-less columnar storage format. Provide flexible representation like JSON and efficient reading similar to other columnar storage formats.
Java
66
star
8

XCMetricsAggregator

Automation tool for Xcode Metrics Organizer with AppleScript
Ruby
62
star
9

YJCaptions

60
star
10

bakusoku-jsonp

Codeless Blog Widgets framework
JavaScript
60
star
11

AppFeedback-ios

📸 You can post feedback messages and screenshots to Slack from your iOS app! 🎥
Objective-C
42
star
12

ngtd

Serving NGT over HTTP or gRPC ※This project is not maintained. We have moved to a new product, [Vald](https://vald.vdaas.org) .
Go
38
star
13

k2hash

K2HASH - NoSQL Key Value Store(KVS) library
C++
37
star
14

authorization-proxy

Moved to https://github.com/AthenZ/authorization-proxy
Go
35
star
15

presto_exporter

Go
34
star
16

k2hftfuse

File transaction by FUSE-based file system
C++
32
star
17

gongt

NGT Go client library
Go
28
star
18

fullock

Fast User Level LOCK library
C++
26
star
19

yjlogin-ios-sdk

Yahoo! JAPAN Login iOS SDK
Swift
26
star
20

ja-vg-vqa

26
star
21

gpu-monitoring-exporter

Prometheus exporter for GPU process metrics.
Shell
26
star
22

jenkins-with-docker-demo

Shell
25
star
23

geobleu

Python implementation of GEO-BLEU, a similarity evaluation method for trajectories
Python
22
star
24

lcom4go

Compute LCOM4, Lack of Cohesion of Methods metrics ver.4, for golang projects.
Go
21
star
25

yconnect-php-sdk

YConnect PHP SDK
PHP
21
star
26

vespa-tutorial

Japanese tutorial for Vespa
Shell
20
star
27

AppFeedback-android

📸 You can post feedback messages and screenshots to Slack from your Android app! 🎥
Java
20
star
28

presto-audit

THIS REPOSITORY IS DEPRECATED
Java
19
star
29

garm

Garm is k8s authorization webhook (SubjectAccessReview API) server for Athenz. Moved to https://github.com/AthenZ/garm
Go
17
star
30

chmpx

Consistent Hashing Mq inProcess data eXchange
C++
17
star
31

docker-continuous-integration-workflow

2014/02/12 Docker Meetup in Tokyo #1 での発表内容です。
Ruby
17
star
32

MultitaskingSample

iOS 7の新機能、BackgroundFetch, SilentPushNotification, BackgroundTransferを利用したサンプルコードです。
Objective-C
16
star
33

athenz-authorizer

athenz policy management library for golang. Moved to https://github.com/AthenZ/athenz-authorizer
Go
15
star
34

athenz-client-sidecar

Moved to https://github.com/AthenZ/athenz-client-sidecar
Go
15
star
35

vespa-kuromoji-linguistics

Java
15
star
36

k2hdkc

k2hdkc is k2hash based distributed kvs cluster
C++
13
star
37

big3store

Erlang
12
star
38

textwebapi-cookbook

Cookbook for the Text Analysis Web API provided by Yahoo! DEVELOPER NETWORK.
Jupyter Notebook
12
star
39

VFD-Dataset

Python
11
star
40

k2htp_dtor

K2HASH Distributed Transaction Of Repeater
C++
10
star
41

solr-plugin-samples

Java
9
star
42

VSU-Dataset

8
star
43

yconnect-servlet-sdk

YConnect Servlet SDK
Java
8
star
44

DynamicsSample

iOS 7の新機能、UIKit Dynamics、Motion Effectsを利用したサンプルコードです。
Objective-C
6
star
45

ConfigCacheBundle

Symfony ConfigCacheBundle for easier handling of user-defined configuration file cache
PHP
6
star
46

AntPickax

AntPickax provides basic libraries, components and systems
6
star
47

yjlogin-android-sdk

Kotlin
5
star
48

chmpx_nodejs

CHMPX nodejs addon library - Consistent Hashing Mq inProcess data eXchange
C++
5
star
49

k2hr3

K2HR3 - K2Hdkc based Resource and Roles and policy Rules
5
star
50

yosegi-spark

Java
5
star
51

hubot-shuffle

hubot-shuffle add shuffle system.
CoffeeScript
5
star
52

yosegi-hive

This is Yosegi's Hive plugin. This can write and read tables with Hive.
Java
5
star
53

k2hr3_osnl

K2HR3 OpenStack Notification Listener - K2Hdkc based Resource and Roles and policy Rules
Python
4
star
54

embulk-output-solr

Java
4
star
55

fastlane-plugin-setup_app_feedback_sdk

Fastlane plugin that update Info.plist for AppFeedback SDK
Ruby
4
star
56

k2hdkc_dbaas

Database as a Service for K2HDKC
Python
4
star
57

k2hash_phpext

PHP Extension library for K2HASH
C
4
star
58

k2hr3_utils

K2HR3 Utils - Utils for K2Hdkc based Resource and Roles and policy Rules
Shell
4
star
59

k2hr3_app

K2HR3 Web Application - K2Hdkc based Resource and Roles and policy Rules
JavaScript
4
star
60

k2hr3_api

K2HR3 REST API - K2Hdkc based Resource and Roles and policy Rules
JavaScript
4
star
61

k2htp_mdtor

K2Hash Transaction Plugin for Multiple Distributed Transaction Of Repeater
Shell
4
star
62

k2hr3_helm_chart

Helm Chart for K2HR3
Shell
3
star
63

k2hdkc_java

K2HDKC Java library - k2hash based distributed kvs cluster
Java
3
star
64

k2hdkc_go

K2HDKC Go library - k2hash based distributed kvs cluster
Go
3
star
65

yosegi-tools

Java
3
star
66

k2hash_go

K2HASH Go library - NoSQL Key Value Store(KVS) library
Go
3
star
67

yj-ci-dataset

3
star
68

k2hr3_cli

K2HR3 Command Line Interface
Shell
3
star
69

embulk-parser-xml2

Java
3
star
70

k2hr3_sidecar

K2HR3 Container Registration Sidecar - K2Hdkc based Resource and Roles and policy Rules
Shell
3
star
71

k2hdkc_python

K2HDKC Python library - k2hash based distributed kvs cluster
Python
3
star
72

k2hash_python

K2HASH Python library - NoSQL Key Value Store(KVS) library
Python
3
star
73

yosegi-hadoop

Java
3
star
74

k2hdkc_nodejs

K2HDKC nodejs addon library - k2hash based distributed kvs cluster
JavaScript
3
star
75

k2hash_nodejs

K2HASH nodejs addon library - NoSQL Key Value Store(KVS) nodejs library
JavaScript
3
star
76

k2hash_java

K2HASH Java library - NoSQL Key Value Store(KVS) library
Java
3
star
77

yosegi-avro

Java
2
star
78

k2hdkc_dbaas_override_conf

K2HDKC DBaaS Override Configuration
Shell
2
star
79

k2hdkc_dbaas_k8s_cli

K2HDKC DBaaS on kubernetes Command Line Interface - K2HR3 CLI Plugin
Shell
2
star
80

k2hr3_get_resource

K2HR3 Utilities - Get K2HR3 Resource Helper for Systemd service
Shell
2
star
81

k2hdkc_dbaas_cli

K2HDKC DBaaS Command Line Interface - K2HR3 CLI Plugin
Shell
2
star
82

hubot-package-version-release

publish release on GitHub based package.json
CoffeeScript
2
star
83

k2hdkc_helm_chart

Helm Chart for K2HDKC DBaaS
Shell
2
star
84

k2hr3client_python

k2hr3client_python is an official Python WebAPI client for k2hr3.
Python
2
star
85

k2hdkc_phpext

PHP Extension library for K2HDKC
PHP
1
star
86

yosegi-example

Java
1
star
87

chmpx_phpext

PHP Extension library for CHMPX
PHP
1
star
88

yosegi-legacy

Java
1
star