cell spacing in custom view cell - ios

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.

Related

In my app I used uicollectionview then how to make difference between two cell same in all iPhone models?

In my app, I used UIcollectionview then how to make difference between two cell same in all iPhone models. and auto layout is turned on I tried many things but the issue is not resolved my app looks perfect on iPhone 5s but It shows more difference between two cells in iPhone 6s and 6s plus.enter image description here
You need to calculate the cell dynamically according to the screen size. I have faced such situation and fixed it. You can try this, might be help you...
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(width, height);
}
get width and height
-(void)getDynamicCellSize {
width = (SCREEN_SIZE.width - 50)/3;
height = (SCREEN_SIZE.height - (135+self.tabBarController.tabBar.frame.size.height)-29)/3;
} // 135 means the navigation bar height + segment control (I have in my design)
CGFloat width, height;// Declare globally
Call getDynamicCellSize method in viewWillAppear or didLoad
OR
Use this code in sizeForItemAtIndexPath method in 'SWIFT'
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
let cellWidth = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right) - flowLayout.minimumInteritemSpacing) / 2
return CGSize(width: cellWidth, height: cellWidth)
Happy coding...

Size of UICollectionView is sometimes wrong

Here is my problem: I'm developing a Kodi Remote on iOS (http://forum.kodi.tv/showthread.php?tid=235973) and everything run pretty well (a classic development) but I'm having a behavior that I can't explain.
I have multiple view controllers; in one of them (a UIViewController containing a UICollectionView), I implemented this delegate method :
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize originalSize = [(UICollectionViewFlowLayout *)collectionViewLayout itemSize];
CGSize collectionViewSize = [collectionView frame].size;
CGFloat interItemSpacing = [(UICollectionViewFlowLayout *)collectionViewLayout minimumInteritemSpacing];
UICollectionViewFlowLayout *collectionViewFlowLayout = (UICollectionViewFlowLayout *)[[self collectionView] collectionViewLayout];
UIEdgeInsets sectionInset = [collectionViewFlowLayout sectionInset];
CGFloat usableWidth = (collectionViewSize.width - ((kColumns - 1) * interItemSpacing) - sectionInset.left - sectionInset.right);
CGFloat width = usableWidth / kColumns;
CGFloat height = width * originalSize.height / originalSize.width;
return CGSizeMake(width, height);
}
I get the collection view frame (bounds is the same, of course, I don't need origin) and it works great. I get on an iPhone 5 simulator 288 width for a no matter height.
BUT, in a copy-paste of the view controller in the storyboard, I get a 304 width. The weirdest behavior is that with FLEX, and after the render, the collection view measures 288 pixels.
The "only" difference is that the first view controller is just pushed, the second is contained in a UITabBarController.
PS : the first screen I'm talking about are the 3rd & 4th screenshots.
If someone has an explication to this, I'll take it ;)
Update 1 :
If I invalidate the collection view layout in - (void)viewDidLayoutSubviews, items are correctly drawn BUT there is like a flash because the whole collection view is redrawn (the header & items below).
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(self.collectionView.frame.size.width,
self.collectionView.frame.size.height);
}
and uncheck "Constrain to margins" and click on all dotted red constrain
so -8 padding will be gone in iPhone 6+
Storyboard
It makes a lot of sense when you approach the problem with a Storyboard. First, you will get a clear view of what you are designing. Second, you get to build your application with exactly 0 lines of code, leading to 0 bugs.
The parent UITabBarController and a set of UINavigationController is a common design pattern, and only when you implement it as follow do you get a proper UI behavior.
Code
You do not have to use a Storyboard to create the hierarchy above. You can embed each UIViewController in a UINavigationController programatically.
► Find this solution on GitHub and additional details on Swift Recipes.

UICollectionViewFlowLayout's estimatedItemSize breaks scrolling?

(EDIT : it seems to be working fine starting with iOS 9. I did not make extensive tests, but the example works. This confirms the bug present in iOS 8.)
I spent a lot of time testing UICollectionView's Flow Layout self sizing behavior. After a lot of frustration the issue is narrowed down to the fact that as soon as one sets the estimatedItemSize to a non-zero size, the scrolling no longer works properly.
In my example instead of showing 40 items it only displays 32.
I've copy pasted the code bellow. I've tested many things starting with a Swift version.
Basically it fails to calculate and/or properly update the layout's collectionViewContentSize()
Here is a complete demo http://git.io/AIrHNA
Anybody can point me in the right direction?
Thank you
#implementation ViewControllerObjcC
static NSString * const reuseIdentifier = #"Cell";
-(UICollectionViewFlowLayout*)flowLayout{
return (UICollectionViewFlowLayout*)self.collectionViewLayout;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
CGSize estimatedSize = CGSizeMake(self.view.frame.size.width, 25.0);
BOOL testEstimatedItemSize = true;
if (testEstimatedItemSize) {
[self flowLayout].estimatedItemSize = estimatedSize;
}else{
[self flowLayout].itemSize = estimatedSize;
}
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 40;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
[cell.contentView addSubview:label];
label.text = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
label.backgroundColor = [UIColor redColor];
return cell;
}
From Apple's Document,
Specifically, cells that are not onscreen are assumed to be the estimated height.
This is my guess.
When the estimated height is smaller than the actual cell height, the collection view's content size prediction is smaller than the actual content size.
Therefore it display only displays 32 of the total 40 cells.
In my project, all celsl are shown when a large estimated size is used.
From the documentation:
The default value of this property is CGSizeZero. Setting it to any
other value causes the collection view to query each cell for its
actual size using the cell’s
preferredLayoutAttributesFittingAttributes: method. If all of your
cells are the same height, use the itemSize property, instead of
this property, to specify the cell size instead.
It seems that you don't use this preferredLayoutAttributesFittingAttributes: method in your project?, have you tried implementing it?
On the other hand, this solution looks good:
Specifying one Dimension of Cells in UICollectionView using Auto Layout
I had the same issue. In my case, the space between items was 32. And my code was like this:
collectionViewLayout.minimumInteritemSpacing = 0
collectionViewLayout.minimumLineSpacing = 32
collectionViewLayout.scrollDirection = .horizontal
collectionViewLayout.itemSize = UICollectionViewFlowLayout.automaticSize
collectionViewLayout.estimatedItemSize = CGSize(width: 100, height: 32)
I changed minimumInteritemSpacing to 32 the same as minimumLineSpacing to fix the problem.
collectionViewLayout.minimumInteritemSpacing = 32

Cell spacing in UICollectionView minimumInteritemSpacing

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);
}

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;
}

Resources