Last active
August 29, 2015 14:22
-
-
Save mxpr/f7a6bc3344b203bd75d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) | |
{ | |
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) | |
collectionView?.reloadData() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) | |
{ | |
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) | |
flowLayout.invalidateLayout() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.matrixprojects.net/p/uicollectionviewcell-dynamic-width