Skip to content

Instantly share code, notes, and snippets.

@mxpr
Last active August 29, 2015 14:22
Show Gist options
  • Save mxpr/f7a6bc3344b203bd75d7 to your computer and use it in GitHub Desktop.
Save mxpr/f7a6bc3344b203bd75d7 to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
class ItemSizeCollectionViewcontroller : UICollectionViewController
{
// MARK: Outlets
@IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
// MARK: Properties
var dataSource = CommonDataSource()
// MARK: UIViewController
override func viewDidLoad()
{
super.viewDidLoad()
let nib = UINib(nibName: "MyCollectionViewCell", bundle: NSBundle.mainBundle())
// Manually set the size to be the same as the collection view width
// flowLayout.itemSize = CGSize(width: collectionView!.bounds.width, height: 60)
// For completeness the section insets need to be accommodated
var width = collectionView!.bounds.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right
flowLayout.itemSize = CGSize(width: width, height: 60)
collectionView?.registerNib(nib, forCellWithReuseIdentifier: "cell")
collectionView?.dataSource = dataSource
}
// MARK: Actions
@IBAction func didTapRefresh(sender: UIBarButtonItem)
{
collectionView?.reloadData()
}
}
import Foundation
import UIKit
class FlowLayoutDelegateCollectionViewcontroller : UICollectionViewController, UICollectionViewDelegateFlowLayout
{
// MARK: Outlets
@IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
// MARK: Properties
var dataSource = CommonDataSource()
// MARK: UIViewController
override func viewDidLoad()
{
super.viewDidLoad()
let nib = UINib(nibName: "MyCollectionViewCell", bundle: NSBundle.mainBundle())
collectionView?.registerNib(nib, forCellWithReuseIdentifier: "cell")
collectionView?.dataSource = dataSource
}
// MARK: Actions
@IBAction func didTapRefresh(sender: UIBarButtonItem)
{
collectionView?.reloadData()
}
// MARK: UICollectionViewDelegateFlowLayout
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
var width = collectionView.bounds.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right
return CGSize(width: width, height:60)
}
}
// MARK: UICollectionViewDelegateFlowLayout
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
var width = collectionView.bounds.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right
return CGSize(width: width, height:60)
}
import Foundation
import UIKit
class FlowLayoutWithTweaksCollectionViewController : UICollectionViewController, UICollectionViewDelegateFlowLayout
{
// MARK: Outlets
@IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
// MARK: Properties
var dataSource = CommonDataSource()
// MARK: UIViewController
override func viewDidLoad()
{
super.viewDidLoad()
let nib = UINib(nibName: "MyCollectionViewCell", bundle: NSBundle.mainBundle())
collectionView?.registerNib(nib, forCellWithReuseIdentifier: "cell")
collectionView?.dataSource = dataSource
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition({ (context) -> Void in
}, completion: { (context) -> Void in
self.flowLayout.invalidateLayout()
})
}
// MARK: Actions
@IBAction func didTapRefresh(sender: UIBarButtonItem)
{
collectionView?.reloadData()
}
// MARK: UICollectionViewDelegateFlowLayout
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
var width = collectionView.bounds.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right
return CGSize(width: width, height:60)
}
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
collectionView?.reloadData()
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
flowLayout.invalidateLayout()
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition({ (context) -> Void in
}, completion: { (context) -> Void in
self.flowLayout.invalidateLayout()
})
}
var widthToUse : CGFloat?
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
widthToUse = size.width
flowLayout.invalidateLayout()
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
var collectionViewWidth = collectionView.bounds.width
if let w = widthToUse
{
collectionViewWidth = w
}
var width = collectionViewWidth - flowLayout.sectionInset.left - flowLayout.sectionInset.right
return CGSize(width: width, height:60)
}
class FullWidthCellFlowLayout : UICollectionViewFlowLayout
{
// MARK: Overrides
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
var attributes = super.layoutAttributesForElementsInRect(rect) as! [UICollectionViewLayoutAttributes]
updateAttribute(attributes)
return attributes
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
var attributes = super.layoutAttributesForItemAtIndexPath(indexPath)
updateAttribute([attributes])
return attributes
}
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
if !CGRectEqualToRect(newBounds, self.collectionView!.bounds)
{
return true
}
return false
}
// MARK: Private
private func updateAttribute(attributes: [UICollectionViewLayoutAttributes])
{
for attr in attributes
{
attr.frame.size.width = fullWidth()
attr.frame.origin.x = 0
}
}
func fullWidth() -> CGFloat
{
return self.collectionView!.bounds.width - self.sectionInset.left - self.sectionInset.right
}
}
import Foundation
import UIKit
class CommonDataSource : NSObject, UICollectionViewDataSource
{
// MARK: UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
{
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 10
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! MyCollectionViewCell
cell.textLabel.text = " cell: \(indexPath.section) - \(indexPath.item)"
return cell
}
}
@mxpr
Copy link
Author

mxpr commented Jun 1, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment