I have a collection view,in which i have a cell.On story board height of cell is equal to the height of collection view.But when i run it shows me the bottom space.Please tell me how can i remove that.I have used following code to remove this.
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
[self setAutomaticallyAdjustsScrollViewInsets:NO];
// return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right
if (collectionView==self.album_collection_view)
{
return UIEdgeInsetsMake(0,10,0,10);
}// top, left, bottom, right
else
return UIEdgeInsetsMake(0,10,0,10);
}
Please tell how to remove it?
I am assuming that there is only one row of cells. If so you can try to set the cell height in collectionView:cellForItemAtIndexPath:.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
...
CGRect newFrame = cell.frame;
newFrame.size.height = CGRectGetHeight(collectionView.frame);
cell.frame = newFrame;
return cell;
}
Related
This question already has answers here:
Left Align Cells in UICollectionView
(24 answers)
Closed 5 years ago.
How to remove space between two cells in UiCollectionView? Min spacing is not working.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView.tag==101)
{
return CGSizeMake((_collView.frame.size.width),(_collView.frame.size.height));
}
else
{
return CGSizeMake((_collView2.frame.size.width/3)-20,300);
}
}
But this is not working.
*
*
Set Min Spacing for cells and for Lines to Zero
and replace your code by
- (CGSize)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *)indexPath
{
if (collectionView.tag==101)
{
return CGSizeMake((_collView.frame.size.width),(_collView.frame.size.height));
}
else
{
return CGSizeMake((_collView2.frame.size.width/3),300);
}
}
Use UICollectionViewFlowLayout for those purposes:
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(cellWidth, cellHeight);
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;
This happens because of how UICollectionviewFlowLayout works. You could provide a custom collectionview layout:
Left Align Cells in UICollectionView
Or use a 3d party solution:
https://github.com/mokagio/UICollectionViewLeftAlignedLayout
use this code, based on your design adjust the width and height.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *)indexPath {
CGSize s = CGSizeMake([[UIScreen mainScreen] bounds].size.width/2-10,
[[UIScreen mainScreen] bounds].size.height/2.9-7);
return s;
}
bellow code adjust the edges of collection view
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(2, 6,2, 6);
}
Use both delegates and change the width and height
A method called minimumInteritemSpacingForSectionAtIndex from UICollectionViewFlowLayoutDelegate could help you. The minimunIteritemSpace set the space beetween items horizontally, for example try this code:
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0; // This is the minimum inter item spacing, can be more
}
Also you can use the edgeInsets to change the space beetween items.
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 10.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 10.0;
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
// return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right
return UIEdgeInsetsMake(20,20,20,20); // top, left, bottom, right
}
I have added the UICollectionView in my app. It works fine but the issue is the spring between the cells is too much. I want to give equal spacing between the cells. On the iPad device I want spaces to be more stretched. Please tell me how can i do this?
Try this:
// Configure layout
UICollectionViewFlowLayout *flowLayout_D = [[UICollectionViewFlowLayout alloc] init];
[flowLayout_D setItemSize:CGSizeMake(102, 96)]; //your cell size
[flowLayout_D setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self->mycollectionView setCollectionViewLayout:flowLayout_D];
and you have delegate methods to change the spacing:
#pragma mark Collection view layout things
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
if (collectionView==myCollectionView) {
return 2.0;
}
else
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
if (collectionView==myCollectionView) {
return 2.0;
}
else
return 0;
}
You can also change the edge insets:
// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right
if (collectionView==myCollectionView) {
return UIEdgeInsetsMake(0,0,0,0);
}// top, left, bottom, right
else
return UIEdgeInsetsMake(0,0,0,0);
}
I am using uicollection view to display the images in grid view format.I am able to display the cells but the height & width is not increased for iPad it only display as the small blocks.Please tell me how can i make it dynamic.
[![enter image description here][1]][1]
I have used following code for make it dynamic.
pragma mark Collection view layout things
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
if (collectionView==self.album_collection_view) {
return 2.0;
}
else
return 2.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
if (collectionView==self.album_collection_view) {
return 2.0;
}
else
return 2.0;
}
// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right
if (collectionView==self.album_collection_view) {
return UIEdgeInsetsMake(5,0,0,0);
}// top, left, bottom, right
else
return UIEdgeInsetsMake(5,0,0,0);
}
Please tell me how can i resolve this issue?
here is the solution
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
YourCollectionCell * cell = (YourCollectionCell *) [YourCollectionViewObj cellForItemAtIndexPath:indexPath];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"YourCollectionCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
cell.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 20);
// SET YOUR CONTENT
[cell layoutIfNeeded];
}
CGSize CellSize = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize withHorizontalFittingPriority:UILayoutPriorityDefaultHigh verticalFittingPriority:UILayoutPriorityDefaultLow];
return CellSize;}
In case above solution not meet your needs,To obtain what you want, you should create your own UICollectionViewLayout (or maybe UICollectionViewFlowLayout) subclass, where you perform all the computations needed for placing every item in the right frame.
Take a look here for a great tutorial and here for something similar to what you want.
If you want to increase cell height and width you have to use sizeForItemAtIndexPath method. for that you have to add UICollectionViewDelegateFlowLayout delegate.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(100, 100);
}
I need to add some space at the bottom of a collectionView, im trying with some footer, this is not working yet,
- (void)viewDidLoad
{
[super viewDidLoad];
[self initTestDataSource];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[DOPCollectionViewCell class] forCellWithReuseIdentifier:#"DOPCollectionViewCell"];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:UICollectionElementKindSectionFooter];
DOPCollectionViewLayout* customLayout = (DOPCollectionViewLayout*)self.collectionView.collectionViewLayout;
[self.collectionView setCollectionViewLayout:customLayout];
}
I placed break points in these but are not being called,
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
referenceSizeForFooterInSection:(NSInteger)section
{
NSLog(#"sizing footer");
return CGSizeMake(320,100);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView * view = nil;
return view;
NSLog(#"Something is happening with the footer...");
}
I just need some transparent space on the bottom of the collection view,
is this the right way to do it?, why is not working ? thanks
Simple way to add space to bottom of entire collection view is using contentInset from UIScrollView:
self.collectionView.contentInset = UIEdgeInsetsMake(top, right, bottom, left);
Where top, right, bottom, left are float values you decide.
I am attempting to lay my UICollectionView out such that it is a 2x3 grid per page. Here is what I am getting so far:
As you can see, the next "page" is peeping in from the right, which I don't want. I figured if I know my width is always going to be 320pts (iPhone only), then I would want my cells to be 130pts in width with 15pts on either side.
Code for layout delegate:
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(130, 130);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(20.0f, 15.0f, 20.0f, 15.0f);
}
And also cellForItemAtIndexPath:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CustCollectionViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:kReuseID
forIndexPath:indexPath];
//Set up list name label
cell.LblListName.font = [self getDefaultFontWithSize:15.0f];
cell.LblListName.textColor = fontAndTertiaryColor;
cell.LblListName.textAlignment = NSTextAlignmentCenter;
cell.LblListName.text = #"Failtron";
//cell.LblListName.adjustsFontSizeToFitWidth = YES;
cell.backgroundColor = secondaryColor;
cell.parallaxIntensity = kParallaxMotion;
[Utilities createViewBorder:cell];
return cell;
}
I thought an inset would act like padding in CSS but I believe I'm off the mark with that one, I should also note that in the UIStoryboard my layout is set to flow. Please let me know if I am missing any relevant code, this is my first attempt with a UICollectionView. I would like this to scroll horizontally as well.
Ok, there are tons of ways to manipulate layouts for a UICollectionView, but sounds like since you are using "flow" you can just implement <UICollectionViewDelegateFlowLayout> in your .h and then use the following methods to tweak your layout to your liking:
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// Size of the cell
CGSize retval = CGSizeMake(130,130);
return retval;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section {
//top, left, bottom, right
return UIEdgeInsetsMake(0, 20, 0, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//Minimum spacing between cells
CGFloat interimSpacing = 0.0f;
return interimSpacing;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section {
// Minimum spacing between lines. In your case,
this is the distance between columns
// If your scroll direction was vertical,
this would be the distance between rows
CGFloat lineSpacing = 20.0f;
return lineSpacing;
}
I've prepopulated this code with some values that should be pretty darn close to what you wanted to accomplish.
Alternatively, all of this can be done in Interface Builder by selecting the UICollectionView and adjusting the values in the Size Inspector, albeit more confusing in my opinion.