UICollectionViewLayout (Converting from Swift to Objective-C) - ios
I'm currently following this tutorial (https://www.raywenderlich.com/99087/swift-expanding-cells-ios-collection-views) which creates a custom UICollectionViewLayout, the problem is that the tutorial is written in Swift but I'm trying to convert it to Objective-C for my project.
The first part of reproducing the tutorial into Objective-C was fine and i've gotten to this stage.
Though, in the 2nd part when we're suppose to create a custom UICollectionViewLayout, and upon changing in the storyboard the Layout of CollectionView to Custom and setting the custom class, a blank screen appears.
Below are the codes that I'd reproduced from the tutorial from Swift to Objective-C:
#implementation TimetableCustomLayout{
NSMutableArray *cache;
}
-(NSInteger)featuredItemIndex{
CGFloat dragOffset = 180;
return MAX(0, self.collectionView.contentOffset.y - dragOffset);
}
-(CGFloat)nextItemPercentageOffset{
CGFloat dragOffset = 180;
return (self.collectionView.contentOffset.y / dragOffset) - [self featuredItemIndex];
}
-(CGFloat)width{
return CGRectGetWidth(self.collectionView.bounds);
}
-(CGFloat)height{
return CGRectGetHeight(self.collectionView.bounds);
}
-(NSInteger)numberOfItems{
//Will be replaced with dynamic value later
return 5;
}
-(CGSize)collectionViewContentSize{
CGFloat dragOffset = 180;
return CGSizeMake([self height], [self width]);
}
-(void)prepareLayout{
[super prepareLayout];
cache = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];
self.standardHeight = 100;
self.featuredHeight = 280;
CGRect frame = CGRectZero;
CGFloat y = 0;
for(int item = 0; item < 5; item ++){
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.zIndex = item;
CGFloat height = self.standardHeight;
if(indexPath.item == [self featuredItemIndex]){
CGFloat yOffset = self.standardHeight * [self nextItemPercentageOffset];
y = self.collectionView.contentOffset.y - yOffset;
height = self.featuredHeight;
}else if(indexPath.item == ([self featuredItemIndex] + 1) && indexPath.item != [self numberOfItems]){
CGFloat maxY = y + self.standardHeight;
height = self.standardHeight + MAX((self.featuredHeight - self.standardHeight) * [self nextItemPercentageOffset], 0);
y = maxY - height;
}
frame = CGRectMake(0, y, [self width], [self height]);
attributes.frame = frame;
[cache addObject:attributes];
y = CGRectGetMaxY(frame);
}
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
[super layoutAttributesForElementsInRect:rect];
NSMutableArray *layoutAttributes = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];
NSLog(#"%#", cache);
for(UICollectionViewLayoutAttributes *attributes in cache){
if(CGRectIntersectsRect(attributes.frame, rect)){
[layoutAttributes addObject:attributes];
}
}
return layoutAttributes;
}
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
return true;
}
#end
I'm also getting error, Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UICollectionViewLayoutAttributes frame]: unrecognized selector.
I believe that the error is possibly due to incorrect translation from Swift to Objective-C, particularly this line,
Swift:
var cache = [UICollectionViewLayoutAttributes]()
Objective-C:
NSMutableArray *layoutAttributes = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];
This is my first question on StackOverflow, any feedback and help would be greatly appreciated, thanks in advance.
You are adding a reference to the UICollectionViewLayoutAttributes class to the cache and layoutAttributes array on initialization. And then, calling frame property to the class reference (notice the + on the error message that denotes a class method, where instance methods use -).
Replace this line:
NSMutableArray *layoutAttributes = [[NSMutableArray alloc]initWithObjects:[UICollectionViewLayoutAttributes class], nil];
With this:
NSMutableArray *layoutAttributes = [[NSMutableArray alloc] init];
The same applies to the cache variable initialization.
You can also use type-safe arrays in Objective-C using generics:
NSMutableArray<UICollectionViewLayoutAttributes*> *layoutAttributes = [[NSMutableArray alloc] init];
More information here.
Related
How to implement animation like Feedly , insorts news app in iOS objective-c
I want to implement an animation like insorts and feedly news app do. I found swift version it is achieve by uicollectionview layout customization . Here is link Depth Page transform on iOS I convert swift to objective c but not achieve same effect. Here is my code import "DepthLayout.h" #interface DepthLayout() { CGFloat contentWidth; CGFloat contentHeight; CGFloat yOffset; CGFloat maxAlpha; CGFloat minAlpha; CGFloat widthOffset; CGFloat heightOffset; NSMutableArray * cache; } #end #implementation DepthLayout #pragma mark - Lifecycle - (id)init { self = [super init]; if (self) { [self setup]; } return self; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; if (self) { [self setup]; } return self; } - (void)setup { yOffset=0; maxAlpha=1; minAlpha=0; widthOffset=35; heightOffset=35; cache=[[NSMutableArray alloc]init]; } -(CGFloat)itemWidth{ return CGRectGetWidth(self.collectionView.bounds); } -(CGFloat)itemHeight{ return CGRectGetHeight(self.collectionView.bounds); } -(CGFloat)collectionViewHeight{ return CGRectGetHeight(self.collectionView.bounds); } -(NSInteger)numberOfItems{ return [self.collectionView numberOfItemsInSection:0]; } -(CGFloat)dragOffset{ return CGRectGetHeight(self.collectionView.bounds); } -(NSInteger)currentItemIndex{ return MAX(0,(NSInteger)(self.collectionView.contentOffset.y/[self collectionViewHeight])); } -(CGFloat)nextItemBecomeCurrentPercentage{ return (self.collectionView.contentOffset.y/[self collectionViewHeight])-(CGFloat)[self currentItemIndex]; } #pragma mark - Layout - (void)prepareLayout{ [cache removeAllObjects]; yOffset=0; for(NSInteger item=0;item<[self numberOfItems];item++){ NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; if ((indexPath.item == [self currentItemIndex ]+1) && (indexPath.item < [self numberOfItems])){ attribute.alpha = minAlpha + MAX((maxAlpha-minAlpha) * [self nextItemBecomeCurrentPercentage], 0); CGFloat width = [self itemWidth] - widthOffset + (widthOffset * [self nextItemBecomeCurrentPercentage]); CGFloat height = [self itemWidth] - heightOffset + (heightOffset * [self nextItemBecomeCurrentPercentage]); CGFloat deltaWidth = width/[self itemWidth]; CGFloat deltaHeight = height/[self itemHeight ]; attribute.frame = CGRectMake(0, yOffset, [self itemWidth],[self itemHeight]); attribute.transform = CGAffineTransformMakeScale(deltaWidth, deltaHeight); CGPoint center=self.collectionView.center; center.y=self.collectionView.center.y+self.collectionView.contentOffset.y; center.x=self.collectionView.center.x+self.collectionView.contentOffset.x; attribute.center = center; yOffset += [self collectionViewHeight]; }else{ attribute.frame = CGRectMake(0, yOffset, [self itemWidth],[self itemHeight]); CGPoint center=self.collectionView.center; center.y=self.collectionView.center.y+yOffset; center.x=self.collectionView.center.x; yOffset += [self collectionViewHeight]; } [cache addObject:attribute]; } } -(CGSize)collectionViewContentSize{ contentWidth = CGRectGetWidth(self.collectionView.bounds); contentHeight = (CGFloat)[self numberOfItems] * CGRectGetHeight(self.collectionView.bounds); return CGSizeMake(contentWidth, contentHeight); } -(NSMutableArray *)layoutAttributesForElementsInRect:(CGRect)rect{ NSMutableArray* layoutAttributes = [[NSMutableArray alloc]init]; for (UICollectionViewLayoutAttributes * attribute in cache){ if(CGRectIntersectsRect(attribute.frame, rect)){ [layoutAttributes addObject:attribute]; } } return layoutAttributes; } -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{ return YES; } -(CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity{ NSInteger itemIndex = round(proposedContentOffset.y / ([self dragOffset])); CGFloat yOffsetTemp = itemIndex * CGRectGetHeight(self.collectionView.bounds); return CGPointMake( 0, yOffsetTemp); } -(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath*)indexPath { UICollectionViewLayoutAttributes *attr=[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; return attr; } #end What is the problem ?
No need to Convert swift to objective c. I Use Swift file directly in my Objective C Project that fulfil my requirements. I used UltravisualLayout from Here : Depth Page transform on iOS Thanks Bridging between swift & objective C;
UICollectionView find center cell when content inset is used
I'm trying to figure out how to find the center most cell of my UICollectionView, which is set to only scroll horizontally. I tried this SO: how to get indexPath for cell which is located in the center of UICollectionView but it isn't working out, I think because I'm using the contentInset property of my UICollectionView. What I'm doing is setting the contentInset left and right of the flow layout to exactly 1/2 of self.view's bounds width minus 1/2 of itemSize width. I'm not sure how to calculate the center most cell due to this. I'd appreciate any help offered. Code: CGPoint cellCenter = CGPointMake((self.collectionView.center.x + self.collectionView.contentOffset.x) - (self.sampleFlowLayout.sectionInset.left + self.sampleFlowLayout.sectionInset.right), self.collectionView.center.y); NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:cellCenter]; if (indexPath) { NSInteger tag = [self.collectionView cellForItemAtIndexPath:indexPath].tag; if (tag != self.currentSample) { self.currentSample = tag; self.imageView.image = [self sampleImageWithOption:self.currentSample]; } } - (void)viewDidLoad { self.sampleFlowLayout = [[UICollectionViewFlowLayout alloc]init]; CGSize itemSize = CGSizeMake(63, 63); self.sampleFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.sampleFlowLayout.sectionInset = UIEdgeInsetsMake(0, (self.view.bounds.size.width / 2) - (itemSize.width / 2), 0, (self.view.bounds.size.width / 2) - (itemSize.width / 2)); }
I figured this out myself using help from this answer: https://stackoverflow.com/a/22696037/1533438 Final code: - (void)snapCollectionView:(UICollectionView *)collectionView withProposedOffset:(CGPoint)proposedOffset { CGRect cvBounds = collectionView.bounds; CGFloat halfWidth = cvBounds.size.width * 0.5; CGFloat proposedContentOffsetCenterX = proposedOffset.x + halfWidth; NSArray *attributesArray = [collectionView.collectionViewLayout layoutAttributesForElementsInRect:cvBounds]; UICollectionViewLayoutAttributes *candidateAttributes; for (UICollectionViewLayoutAttributes *attributes in attributesArray) { if (attributes.representedElementCategory != UICollectionElementCategoryCell) { continue; } if (!candidateAttributes) { candidateAttributes = attributes; continue; } if (fabsf(attributes.center.x - proposedContentOffsetCenterX) < fabsf(candidateAttributes.center.x - proposedContentOffsetCenterX)) { candidateAttributes = attributes; } } CGPoint offset = CGPointMake(candidateAttributes.center.x - halfWidth, proposedOffset.y); [collectionView setContentOffset:offset animated:YES]; CGPoint cellCenter = CGPointMake(offset.x + self.sampleCollectionView.bounds.size.width / 2.0, self.sampleCollectionView.bounds.size.height / 2.0); NSIndexPath *indexPath = [self.sampleCollectionView indexPathForItemAtPoint:cellCenter]; if (indexPath) { NSInteger tag = [self.sampleCollectionView cellForItemAtIndexPath:indexPath].tag; if (tag != self.currentSample) { self.currentSample = tag; UIImage *image; if (self.currentSample == 0) { image = self.image; } else { image = [self sampleImageWithOption:self.currentSample]; } dispatch_async(dispatch_get_main_queue(), ^{ self.imageView.image = image; }); } } }
Dynamically resize UICollectionViewCell in non-scrolling UICollectionView
I have a small, single row horizontal layout UICollectionView at the top of the screen. It can contain up to a maximum of 6 items. The problem is that I want all 6 items visible without scrolling (this collection view is also going to be used in a Today extension which doesn't allow scrolling). What I want to do is reduce the cell-size and inter-item spacing a little bit to allow all 6 cells to fit. Basically I'm trying to avoid this: I've been playing with this for a while but I'm not sure how to approach it. I created a method that's fired every time an item is added or removed from the collection view, just before [self.collectionview reloadData] is called. -(void)setupCollectionViewLayout{ UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)self.buttonBarCollectionView.collectionViewLayout; //Figure out if cells are wider than screen CGFloat screenwidth = self.view.frame.size.width; CGFloat sectionInsetLeft = 10; CGFloat sectionInsetRight = 10; CGFloat minItemSpacing = flowLayout.minimumInteritemSpacing; CGSize itemsize = CGSizeMake(58,58); CGFloat itemsizeWidth = itemsize.width; CGFloat totalWidth = sectionInsetLeft + sectionInsetRight + (itemsizeWidth * _armedRemindersArray.count) + (minItemSpacing * (_armedRemindersArray.count -2)); CGFloat reductionAmount = itemsizeWidth; if (totalWidth > screenwidth) { while (totalWidth > screenwidth) { totalWidth = totalWidth - 1; reductionAmount = reductionAmount - 1; } CGSize newCellSize = CGSizeMake(reductionAmount, reductionAmount); flowLayout.itemSize = newCellSize; } else flowLayout.itemSize = itemsize; } This is the result. Not exactly what I was expecting. Not only did it squash everything to the left and also added a second line, but I also seem to have a cell-reuse issue. Truthfully I would just use static-cells if it was even an option, but unfortunately it seems like it's not possible. What should I be doing? Subclassing UICollectionViewFlowLayout? Won't that basically do the same thing I'm doing here with the built-in flow layout? EDIT: Kujey's answer is definitely closer to what I need. I still have a cell-reuse issue though.
Xcode provides an object designed for your need. It's called UICollectionViewFlowLayout and all you need to do is subclass it and place your cells the way you want. The function prepareForLayout is call every time the collection view needs to update the layout. The piece of code you need is below : #import "CustomLayout.h" #define MainCell #"MainCell" #interface CustomLayout () #property (nonatomic, strong) NSMutableDictionary *layoutInfo; #end #implementation CustomLayout -(NSMutableDictionary *) layoutInfo { if (!_layoutInfo) { _layoutInfo = [NSMutableDictionary dictionary]; } return _layoutInfo; } -(void) prepareLayout { NSMutableDictionary *cellLayoutInfo = [NSMutableDictionary dictionary]; NSIndexPath *indexPath; CGFloat itemWidth; CGFloat itemSpacing; CGFloat widthWithoutSpacing = [self collectionViewContentSize].width / ([self.collectionView numberOfItemsInSection:0]); if (widthWithoutSpacing > [self collectionViewContentSize].height) { itemWidth = [self collectionViewContentSize].height; itemSpacing = ([self collectionViewContentSize].width - itemWidth*[self.collectionView numberOfItemsInSection:0])/ ([self.collectionView numberOfItemsInSection:0]+1); } else { itemWidth = widthWithoutSpacing; itemSpacing = 0; } CGFloat xPosition = itemSpacing; for (NSInteger section = 0; section < [self.collectionView numberOfSections]; section++) { for (NSInteger index = 0 ; index < [self.collectionView numberOfItemsInSection:section] ; index++) { indexPath = [NSIndexPath indexPathForItem:index inSection:section]; UICollectionViewLayoutAttributes *itemAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; CGRect currentFrame=itemAttributes.frame; currentFrame.origin.x = xPosition; currentFrame.size.width = itemWidth; currentFrame.size.height = itemWidth; itemAttributes.frame=currentFrame; cellLayoutInfo[indexPath] = itemAttributes; xPosition += itemWidth + itemSpacing; } } self.layoutInfo[MainCell] = cellLayoutInfo; } - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return YES; } - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { NSMutableArray *allAttributes = [NSMutableArray arrayWithCapacity:self.layoutInfo.count]; [self.layoutInfo enumerateKeysAndObjectsUsingBlock:^(NSString *elementIdentifier, NSDictionary *elementsInfo, BOOL *stop) { [elementsInfo enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath, UICollectionViewLayoutAttributes *attributes, BOOL *innerStop) { if (CGRectIntersectsRect(rect, attributes.frame)) { [allAttributes addObject:attributes]; } }]; }]; return allAttributes; } -(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { return self.layoutInfo[MainCell][indexPath]; } -(CGSize) collectionViewContentSize { return self.collectionView.frame.size; } #end You can also change the y origin of your cells if you need to center them vertically.
try with this code. I get the width and use _armedRemindersArray (i guess you use this array for the items). -(void)setupCollectionViewLayout{ UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)self.buttonBarCollectionView.collectionViewLayout; //Figure out if cells are wider than screen CGFloat screenwidth = self.view.frame.size.width; CGFloat width = screenwidth - ((sectionInsetLeft + sectionInsetRight) *_armedRemindersArray.count + minItemSpacing * (_armedRemindersArray.count -2)); CGSize itemsize = CGSizeMake(width,width); flowLayout.itemSize = itemsize; }
I don't know why you're setting the itemsize first, and then reducing it. I think you should do it the other way around: -(void)setupCollectionViewLayout{ UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)self.buttonBarCollectionView.collectionViewLayout; CGFloat sectionInsetLeft = 10; CGFloat sectionInsetRight = 10; CGFloat minItemSpacing = flowLayout.minimumInteritemSpacing; // Find the appropriate item width (<= 58) CGFloat screenwidth = self.view.frame.size.width; CGFloat itemsizeWidth = (screenwidth - sectionInsetLeft - sectionInsetRight - (minItemSpacing * (_armedRemindersArray.count -2))) / _armedRemindersArray.count itemsizeWidth = itemsizeWidth > 58 ? 58 : itemsizeWidth; flowLayout.itemSize = CGSizeMake(itemsizeWidth, itemsizeWidth); } Does this work? If not, could you please include more of your code?
UICollectionViewFlowLayout implementation
I am trying to accomplish the following, I've done this easily with UIScrollview but I've been experimenting with UICollectionView lately (I know I'm pretty late to the game) and would love to know if in order to do what I want I have to implement a custom layout or if FlowLayout already does this for me. Basically, if you look at the attachment, you will notice that scrolling can happen both vertically and horizontally, the rows go all the way to beyond the UICollectionView height. The same happens with columns going beyond the width of the collection view. Is this possible to do with Flowlayout?
I've done it like this. #define space 5 #import "MultpleLineLayout.h" #implementation MultpleLineLayout { // a subclass of UICollectionViewFlowLayout NSInteger itemWidth; NSInteger itemHeight; } -(id)init { if (self = [super init]) { itemWidth = 60; itemHeight = 60; } return self; } -(CGSize)collectionViewContentSize { NSInteger xSize = [self.collectionView numberOfItemsInSection:0] * (itemWidth + space); // "space" is for spacing between cells. NSInteger ySize = [self.collectionView numberOfSections] * (itemHeight + space); return CGSizeMake(xSize, ySize); } - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path { UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path]; attributes.size = CGSizeMake(itemWidth,itemHeight); int xValue = itemWidth/2 + path.row * (itemWidth + space); int yValue = itemHeight + path.section * (itemHeight + space); attributes.center = CGPointMake(xValue, yValue); return attributes; } -(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect { NSInteger minRow = (rect.origin.x > 0)? rect.origin.x/(itemWidth + space) : 0; // need to check because bounce gives negative values for x. NSInteger maxRow = rect.size.width/(itemWidth + space) + minRow; NSMutableArray* attributes = [NSMutableArray array]; for(NSInteger i=0 ; i < self.collectionView.numberOfSections; i++) { for (NSInteger j=minRow ; j < maxRow; j++) { NSIndexPath* indexPath = [NSIndexPath indexPathForItem:j inSection:i]; [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]]; } } return attributes; } My data source was an array of arrays with each inner array providing the data for an individual row. After Edit: My collection view scrolled in both directions. This is what I had in my viewDidload to set things up: - (void)viewDidLoad { self.theData = #[#[#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"], #[#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"],#[#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"],#[#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"],#[#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"],#[#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"],#[#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"],#[#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"],#[#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"],#[#"0",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"],#[#"Z0",#"Z1",#"Z2",#"Z3",#"Z4",#"Z5",#"Z6",#"Z7",#"Z8",#"Z9",#"Z10",#"Z11",#"Z12",#"Z13",#"Z14",#"Z15",#"Z16",#"Z17",#"Z18",#"Z19",#"Z20"]]; MultpleLineLayout *layout = [[MultpleLineLayout alloc] init]; self.collectionView = [[RDCollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]; self.collectionView.dataSource = self; self.collectionView.delegate = self; self.view.backgroundColor = [UIColor blackColor]; [self.view addSubview:self.collectionView]; [self.collectionView registerClass:[DataCell class] forCellWithReuseIdentifier:#"DataCell"]; [self.collectionView reloadData]; }
Is there a way to make a UICollectionView row or header stay still a.k.a. freeze panes?
I'm wondering if there is a way to make a UICollectionView row or header stay still similar to the freeze panes function in a spreadsheet program. I basically want the first column and row to stay still while the rest is pannable. Is this possible to do with the UICollectionView?
I have found that the following code will set the first row/section as always visable and will scroll with the appropriate section/row: - (NSArray *) layoutAttributesForElementsInRect: (CGRect) rect { NSMutableArray *attributes = [NSMutableArray array]; for (NSInteger section = 0; section < self.collectionView.numberOfSections; section++) for (NSInteger item = 0 ; item < [self.collectionView numberOfItemsInSection: section]; item++) { UICollectionViewLayoutAttributes *layout = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:section]]; if(section==0 || item==0) { UICollectionView * const cv = self.collectionView; CGPoint const contentOffset = cv.contentOffset; CGPoint origin = layout.frame.origin; if(item==0) { origin.x = contentOffset.x; layout.zIndex = 1022; } if(section==0) { origin.y = contentOffset.y; layout.zIndex = 1023; if(item==0)layout.zIndex = 1024; } layout.frame = (CGRect) { .origin = origin, .size = layout.frame.size }; } [attributes addObject:layout]; } return attributes; }