Cell spacing in UICollectionView minimumInteritemSpacing - ios

How to do set cell spacing in a section of UICollectionView? UICollectionView there is a property minimumInteritemSpacing, I need to set 1.0 still it not work. And I implemented the delegate method.
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 1.0;
}

To check about how your collection Cells would look, you can try this in your Storyboard with the help of storyboard. First just for the sake of checking, put some static cells in your CollectionViewController like this so that your screen appears like this :
No you can see those cells and the spacing between them. In your case, the cells will appear with improper spacing as you have shown above. So now open with this screen open, open up the Size Inspector fron your Interface Builder. It would look something like this :
Now you can see some options in the size inspector window. You can adjust the size of each cell and also the spacing between them using the Min. Spacing option. And finally for equal spacing from left and right sides, use the Section Insets Option. As you change the values there, the change will be reflected in your Controller View. So you can get an idea if you want to increase/decrease some values.
Hope this helps.

you can set programmatically like this
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
layout.minimumLineSpacing = 0.0;
layout.minimumInteritemSpacing = 0.0;
contestsCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];

This method set up spaces between cells, footer and header:
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(20, 20, 20, 20);
}

Related

cell spacing in custom view cell

I am newbie in iOS developing field. And i am facing trouble in creating custom collectionView cell. I have even tried changing Min spacing in size inspector from 10 to 2. But nothing is working out. Can anybody help me out.
And here is the attribute set for the custom cell.
attribute given for the custom cell
As per my undersanding, you have taken UICollectionView and use it delegate and datasource methods.
With delegate and datasource methods, use below method also.
return CGSizeMake(((self.view.frame.size.width-10(leftpadding)-10(top)-5(right)-10(bottom))/2) ,180);
Also Set Min spacing and Intents in IB of your storyboard.
As per my developing,my view is as below : -
Hope this will help you.
Download this project and import KTCenterFlowlayout.h and .m files into your project then write below code in view did load It Will work Fine
KTCenterFlowLayout *layout = [KTCenterFlowLayout new];
layout.minimumInteritemSpacing = 10.f;
layout.minimumLineSpacing = 10.f;
[self.collectionView setCollectionViewLayout:layout];
u can adjust the spacing between the cells by using like this
layout.minimumInteritemSpacing = 5.0;
layout.minimumLineSpacing = 5.0;
Change your code like this in the sizeForItemAtIndexPath method
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat minSpace = 10; //Space that you want
return CGSizeMake((screeWidth - minSpace) / 2 , cellHeight); //Cell height is your collectionViewCell height.
}
Hope this will help you.

Grouped UITableView 35 point / pixel top inset / padding

Please note, I'm going to refer to points as pixels in this question.
I have a grouped UITableView with 3 sections, each with a 40 pixel tall header view. The first header in the table view seems to be given a y position of 35 pixels by the system.
I've tried messing around with automaticallyAdjustsScrollViewInsets and a few other iOS 7 automatic pieces, but to no avail.
Why is my UITableView content being inset by 35 pixels?
EDIT: I've seen this answer and many other threads on this. I have valid headers and header heights. Also, setting the default to FLT_MIN, 0.01f, 1.0f or 100.0f doesn't fix the problem.
Here is my header implementation:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
UIView *header = (self.headerViews.count > section ? self.headerViews[section] : nil);
return (header.viewHeight ? : FLT_MIN);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header = (self.headerViews.count > section ? self.headerViews[section] : nil);
return (header.viewHeight ? header : nil);
}
I'm also setting:
- (BOOL)automaticallyAdjustsScrollViewInsets{return NO;}
and
[self setSectionHeaderHeight:FLT_MIN];
It seems that this:
[self setTableHeaderView:[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, width, FLT_MIN)]];
In init, where self is a UITableView, will get rid of the top 35 points.
Yep,i get this problem when i set tableview.tableFooterView and custom set section header height, then first section height has over 35 points.
Then i add a
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
worked for me.
There are multiple ways of getting the same issue you are getting and therefore, multiple solutions. I will try to list the ones I know with their corresponding solutions. Most of them are copied from this question.
UITableView inside a UIViewController
This is a common one that trips people off because they think the problem is related to their UITableView and most of the time is actually the parent UIViewController.
If your VC is embedded on a NavigationController. You will get a 35 points y offset as mentioned here.
Solutions
In Xcode Version > 5 on VC untick Extended Edges "Under Top Bars under the Attributes Inspector to remove the top UITableView content inset.
Constraints: Your VC has a main view where all the other subviews are laid including your UITableView. You need to make sure that all constraints from your UITableView are explicitily set and satisfied/non-ambiguous.
Set self.navigationController.navigationBar.translucent = YES;
Set self.automaticallyAdjustsScrollViewInsets = NO;. This one can also be set on the storyboard by unchecking the Adjust Scroll View Insets checkbox for the view controller layout.
Set self.edgesForExtendedLayout = UIRectEdgeNone;
General UITableView/UITableViewController
Set the tableView.separatorInset = UIEdgeInsetsZero;
Set the tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0); Pay attention to the -35. The negative number offsets the view.
Declare this method (Can also be used for FooterSection):
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return CGFLOAT_MIN;
}

How to make UICollectionViewCell fit its subviews (using Auto Layout)

How can I change the size of a UICollectionViewCell based on the size of its subviews, using Auto Layout?
My UICollectionViewCells each only have one view in their contentView. This is a custom UIView subclass of mine that automatically resizes itself to fit all its subviews using Auto Layout. It also implements -intrinsicContentSize correctly. However the UICollectionView, or rather its layout, do not seem to care about this.
I am using a UICollectionViewFlowLayout, and tried:
Leaving the itemSize at its default value. This gives me cells with a size of 50 x 50.
Implementing the delegate method -collectionView:layout:sizeForItemAtIndexPath:. However, when this method gets called, the cells (obviously) have not appeared on screen yet, and have not been updated by the Auto Layout system.
Is there any way to change my cells' size based on their subviews using the standard Apple classes? Or do I have to implement my own UICollectionViewLayout subclass, or something else?
Have you tried this :
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.5 animations:^{
constraintConstantForWidth.constant = subviews.size.width;
constraintConstantForHeight.constant = subviews.size.height;
[self.view layoutIfNeeded];
}];
and make the priority of the constraint (low = 250)
I think that this issue we can handle by the constraint constant replacement.
I used
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
UIButton *button = (UIButton *)[cell viewWithTag:2];
CGRect frame = cell.frame;
frame.size.width = button.frame.size.width;
cell.frame = frame;
}
}
and was able to resize my cell based on the width of the button subview in a little test app.
Make sure that the tags on your subviews are greater than 0 because if you use 0, it just gives you back the contentView. Other than that, this should get you there
Try collectionView:layout:sizeForItemAtIndexPath:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return self.view.frame.size;
}

ios UICollectionView separation on top / bottom between cells

I have a collection view, is working fine, and I have adjusted the separation for the X padding,
and it works fine,
but for the Y padding between cells, doesn't seem to adjust to NO separation
This is my code for the layout
UICollectionViewFlowLayout *layoutItem=[[UICollectionViewFlowLayout alloc] init];
layoutItem.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
So how can I set the separation on top / bottom to 0px? Between cells?,
thanks!
you will see only top y padding at first time.
And For showing bottom y padding you need more data that CollectionView frame height. When you scroll up collection view you will see bottom y padding.
I used collectionView like this
https://stackoverflow.com/a/17856406/1305001
When I set
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(60, 10, 50, 10);
}
The output will come as First time..
When you scrolled up collectionView you will see bottom padding..
Use this for verticle line spacing between cells
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 5;
}
Will look like
You can also do it a much simpler way using the collection view flow layout.
Set up the flow layout (remember to add the delegate in your header):
UICollectionViewFlowLayout * stickerFlowLayout = [[UICollectionViewFlowLayout alloc] init];
stickerFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
**stickerFlowLayout.minimumLineSpacing = 10;**
stickerFlowLayout.minimumInteritemSpacing = 5;
stickerFlowLayout.sectionInset = UIEdgeInsetsMake(10, 25, 20, 25);
// Set up the collection view
collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:stickerFlowLayout];
collectionView.delegate = self;
collectionView.dataSource = self;
etc
As you can see we can easily set the line spacing using:
**stickerFlowLayout.minimumLineSpacing = 10;**
We can also alter the other attributes
stickerFlowLayout.minimumInteritemSpacing = 5;
With the interitem spacing affecting the spaces between items (similar but different to the line spacing)
Using the flow layout you can save on a huge amount of code and set your collection view up programatically all in one place (just setting the inset and line spacing has immediately saved me two extra unneeded functions)

UICollectionView Layout Issue

I am using UICollectionView using the flow layout.
I have made a custom UICollectionViewCell for the same.
But on running the project the console keeps on throwing this error-
The behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less that the height of the UICollectionView minus the section insets top and bottom values.
I have made sure that the size of the cell is correct
Has anyone been able to resolve this issue.
I found that, using storyboards, you have to go into the storyboard and click on the overall View Controller (the view should be highlighted in blue) and go to the Attributes Inspector (the fourth tab on the right side of the screen) and unchecking the "Under Top Bars", "Under Bottom Bars", and "Under Opaque Bars." This cleared up the issue for me, and it cleared it for my coworkers as well.
I've been having some issues when the using a collection view to present full screen view controllers.
Basically at run time, it'll ask for the size of the item and simply return the size of the collection view:
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return self.collectionView.frame.size;
}
I know this is a very late reply, but I have also experienced this.
Inside my view controller, let's call it MyViewController I have a UICollectionView that is using custom UICollectionViewCells. I'm implementing the method collectionView:layout:sizeForItemAtIndexPath where I am returning a item size that is dependent on the height of the UICollectionView.
MyViewController is made in a storyboard using autolayout to control the height of the UIViewController.
The cells look fine when in portrait mode, but did not when in landscape mode.
I solved my issues by invalidating the UICollectionViewLayout of my UICollectionView inside the viewWillLayoutSubviews method.
- (void)viewWillLayoutSubviews {
[self.myCollectionView.collectionViewLayout invalidateLayout];
}
Earlier I had tried to invalidate the layout inside willAnimateRotationToInterfaceOrientation:duration, but it didn't work. The reason for this is probably that the sizes for all the views are not calculated yet, so we have to wait until autolayout has finished its magic. I refer to this SO thread.
Update for Swift 4.0:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.myCollectionView.collectionViewLayout.invalidateLayout()
}
Had this issue myself a few times when trying to create collection views with fullscreen cells. My problem was caused by laying out for 4" screen in IB/Storyboard and specifying 320*568 for item size, but running in the simulator using 3.5" screen, which has a height of 480. The solution is to specify your item size in code with something like:
UICollectionViewFlowLayout *layout = (id) self.collectionView.collectionViewLayout;
layout.itemSize = self.collectionView.frame.size;
This ensures that the cell size is set correctly at runtime.
If you are using storyboards and auto layout, debugging this kind of problems is really hard...
I had similar problem when trying to display UICollectionViewCell fullscreen on iPhone.
Most of the time issue is with the size of the cell set in
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)
or directly on flowLayout.itemSize.
But... try:
Select the ViewController:
Then uncheck Extend Edges options:
And now try setting your auto layout constraints.
Good luck.
That fixed my problem:
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height - 70);
}
U can see padding value from top and bottom on this screen:
Be sure to set the right autoresizing mask for UICollectionView and UICollectionviewcell.
This fixed my problem for iPhone 4 Simulator
For me I am using AKPickerView inside of a custom UIView which was giving me a slew of warnings like the ones mentioned here. All I did to eliminate the warnings was to Un-Check the box called 'AutoResize Subviews' in the Attributes Inspector for my custom UIView. That silenced the warnings so hard, I thought my logger stopped working.
What caused this for me was that I was trying to set my cells to the size of the superview rather than the size of the UICollectionView. Simply replaced the superview frame with the UICollectionView frame.
I had this problem when I was need full screen cells in collectionView. So I resolved it with this decision.
Cell size is equal collection view size
2. Change in storyboard "Inset from:" from Safe Area to Content Inset
Change "Content Insets" to Never
I just ran into the same error message, but for me the issue was that when I received new data from the api and tried to refresh my collectionView, the method that called [collectionView reloadData] wasn't calling it on the main thread.
Hope this helps someone...
I have same issue
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values.
I solved this issue by checking the values in section Insets.And for fix cell size I have used below code.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
CGFloat cellWidth = [[UIScreen mainScreen] bounds].size.width - 20;
CGFloat cellHeight = [[UIScreen mainScreen] bounds].size.height - 120;
return CGSizeMake(cellWidth, cellHeight);
}
If you're loading a collectionView via a container view + UICollectionViewController, you may have set a height constraint on the container view which is constraining the height of the collection view itself. In my case, I found the collectionView.contentSize was taller than the constraint I had set on my container view.
Obviously this is a bit of an edge case but just in case you have a similar setup, I figured I'd add this comment.
For me the solution was to set Estimate Size (in the storyboard size attributes pane) for the collection view to None.
It's complaining that the item height is bigger than the height of the collectionView. So it's sticking out.
So you have to make the height the same or less.
Like this:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.height, height: collectionView.frame.height)
}
Using the height for the width makes it a square. This is assuming inset is 0 which it should be if you haven't done anything.
None of the above fixes did it for me.
I fixed it with this
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{CGFloat currentWidth = collectionView.frame.size.width;
UIEdgeInsets sectionInset = [(UICollectionViewFlowLayout *)collectionView.collectionViewLayout sectionInset]; //here the sectionInsets are always = 0 because of a timing issue so you need to force set width of an item a few pixels less than the width of the collectionView.
CGFloat width = currentWidth - 10;
}

Resources