3

I would like to customize the UICollectionView, but all the tutorials on the web and in books use UICollectionViewController which I do not want to use. I'm combining different kinds of UICollectionViews in the same UIViewController, so I don't want to limit myself by subclassing UICollectionViewController, which doesn't allow me to place several UICollectionViews in the same viewController along with other objects and labels.

How do I subclass UICollectionView? Specifically, what kinds of methods need to be overwritten? How do I layout what goes into it (i.e. where to put a text label, image, etc.)? I'm familiar with the data source / delegate protocols, but not quite sure how to customize the cell's look, placement of sub items within the cell, what classes need to be overwritten/sub classed, etc.

A list of things I need to do would be much appreciated!

Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Erika Electra
  • 1,854
  • 3
  • 20
  • 31

3 Answers3

1

I believe that you don't need to sub-class UICollectionView but you need to sub-class UICollectionViewFlowLayout.

UICollectionViewFlowLayout is the object which manages the display of the collection view items and it is highly customizable.

I suggest you follow WWDC 2012:Advanced Collection Views and Building Custom Layouts

The best tutorial which worked for me was by Apple called Using the Flow Layout.

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
Raz
  • 2,633
  • 1
  • 24
  • 44
  • Ok thanks. One follow-up question: If I customize UICollectionViewFlowLayout instead, who is responsible for ensuring that the embedded view can only scroll horizontally, etc.? My custom UICollectionViewCell can handle layout & placement of the image within the cell, but someone still needs to ensure that every time this UICollectionView is added anywhere, it can *only* scroll left & right, etc. NOTE: I can't let ViewController handle it because that lets other people add my custom layout and forget to restrict the scrolling. – Erika Electra May 12 '14 at 11:41
  • 1
    `scrollDirection` is a property of `UICollectionViewFlowLayout`. So if you are subclassing flow layout, override `-setScrollDirection` to ignore the input and always set it to `UICollectionViewScrollDirectionHorizontal`. – Zev Eisenberg May 12 '14 at 14:48
1

You can do it like this link

Here are some good Examples to customize your collectionview.

Use WaterfallCollectionView from this. It may help you.

Community
  • 1
  • 1
Viral Narshana
  • 1,855
  • 2
  • 21
  • 45
0

I think you wont have to subclass a collection view. What you would need instead is subclassing UICollectionViewLayout to customize the positioning of cells within the collection view. And if you are looking for a customized look of a cell, you would also have to subclass UICollectionViewCell and assign the sub classed cells to the Collection View. The below links might help you,

https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCustomLayouts/CreatingCustomLayouts.html

http://adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial/

Raj
  • 103
  • 11
  • Thanks Raj, but same question as above: If I customize only the layout/positioning and the content within each cell, how do I encapsulate other settings or restrictions for the entire UICollectionView? Ex: if the custom collection view must be restricted to a certain scroll direction, certain background color, certain frame, etc. Not individual cell properties, and not layout properties (?) either, unless there is a way to set these in the custom layout? – Erika Electra May 12 '14 at 11:43
  • The properties you mentioned (background color,scroll direction etc,.) are straight forward and directly accessible from the instance of UICollectionView. If you are only looking to set/get properties with no additional modifications over it, sub classing is just redundant. Nevertheless you can subclass UICollectionView and override the above properties setter methods, eg: -(void) setBackgroundColor:(UIColor*) backgroundColor{} – Raj May 12 '14 at 12:28