Coder Social home page Coder Social logo

squarefrog / collectionviewindex Goto Github PK

View Code? Open in Web Editor NEW

This project forked from crosswaterbridge/collectionviewindex

0.0 2.0 0.0 30 KB

View that replicates the built in UITableView section index, but for use in UICollectionView.

License: MIT License

Ruby 3.03% Objective-C 6.42% Swift 90.55%

collectionviewindex's Introduction

CollectionViewIndex

Pod Version Pod License Pod Platform

View that replicates the built in UITableView section index, but for use in UICollectionView.

UITableView uses a private class called UITableViewIndex to provide this behavior. CollectionViewIndex follows the same naming convention.

Installation

Install with CocoaPods by adding the following to your Podfile:

use_frameworks!

pod 'CollectionViewIndex'

Then run:

pod install

Usage

Create an instance of CollectionViewIndex and add it to the superview of, and in front of, your UICollectionView instance:

override func viewDidLoad() {
	super.viewDidLoad()
	
	view.addSubview(collectionView)
	view.addSubview(collectionViewIndex)
	
	let views: [String: AnyObject] = [
		"topLayoutGuide": topLayoutGuide,
		"bottomLayoutGuide": bottomLayoutGuide,
		"collectionView": collectionView,
		"collectionViewIndex": collectionViewIndex,
	]
	
	view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[collectionView]|", options: [], metrics: nil, views: views))
	view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[collectionView]|", options: [], metrics: nil, views: views))
	view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("[collectionViewIndex]|", options: [], metrics: nil, views: views))
	view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[topLayoutGuide][collectionViewIndex][bottomLayoutGuide]", options: [], metrics: nil, views: views))
}

Like UILabel, CollectionViewIndex needs to know its height before it can know how wide it must be. When using Auto Layout, you can set the object’s preferredMaxLayoutHeight like so:

override func viewWillLayoutSubviews() {
	super.viewWillLayoutSubviews()
	
	collectionViewIndex.preferredMaxLayoutHeight = view.bounds.height - topLayoutGuide.length - bottomLayoutGuide.length
}

Your collection view layout may need to know the width of the CollectionViewIndex object. If so, you can inform it when layout completes:

override func viewDidLayoutSubviews() {
	super.viewDidLayoutSubviews()
	
	collectionViewLayout.indexWidth = collectionViewIndex.bounds.width
}

CollectionViewIndex sends a UIControlEvent.ValueChanged event when the user interacts with it. You register the target-action method in this way:

collectionViewIndex.addTarget(self, action: "selectedIndexDidChange:", forControlEvents: .ValueChanged)

func selectedIndexDidChange(collectionViewIndex: CollectionViewIndex) {
	print("User selected index \(collectionViewIndex.selectedIndex)")
}

You’ll probably want to jump to a section of the collection view in response to that event. If your section headers pin to visible bounds, be sure to take them into account when scrolling.

License

CollectionViewIndex is released under the MIT license. See LICENSE for details.

collectionviewindex's People

Contributors

hiltonc avatar readmecritic avatar squarefrog avatar

Watchers

 avatar  avatar

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.