UICollection has paging just like a UIPageViewController. With the latter, you have UIPageViewControllerOptionInterPageSpacingKey to easily set the spacing. How is this best achieved with a UICollectionView?
You can probably accomplish explicitly with UICollectionViewLayoutAttributes, but my approach (that works for collection views and my own subview-containing scroll views) is to tile the cells with no spacing (which makes the layout math simple), but make the cells transparent.
Then within each cell create a more opaque content view whose frame is an insetRect on the cell's bounds.
You can use the collectionView:layout:insetForSectionAtIndex: method for your UICollectionView. You need to add this code...
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(top, left, bottom, right);
}
it was answered here: UICollectionView spacing margins by #michael23
Related
I'm trying to change minimumLineSpacing for some cells depending on what row they are.
Imagine having 10 cells; I want minimumLineSpacing to be 8 for all of them except for cell number 7.
I tried doing it via delegate:
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
But I just wish that it would pass back a NSIndexPath instead of a section.
I'd appreciate any input.
One workaround:
If you have it subclassed , you should be able to do this very easy with autolayout and a XIB.
Example:
If you have a constraint in the cell to the bottom of the contentView, you can simply set the constraint to an NSLayoutConstraint property and update the constant of the constraint for that specific cell only to give an extra spacing.
To keep the same proportions and height as the rest of the cells, you only add the extra spacing (same as you added to the constant) to the itemSize.height to that specific cell.
If you don't have this subclassed with autolayout or anything. You can do it programatically the same way but with a subView frame CGRect
I'm facing a problem where I want to realize a UICollectionView that essentially builds up from the center.
Let's say I have this view which shows a list of people:
When the user keeps adding multiple persons using the +, I want the CollectionView to expand but stay in the center of the view:
How do I realize this behavior?
If you are not using autolayout and size classes then you can set "center" is a collection view like this...
[collectionView setCenter:self.view.center];
And if autolayout is enabled and you are using it then do this...
Set CenterX and CenterY constrains of collection view
Set Height and width constraints of collection view
Instead of a custom flow layout, an alternative approach would be to have a full height collection view as normal and then set a top and bottom inset to visually center the cells:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
let inset = (collectionView.frame.size.height - ((COUNT_OF_CELLS / CELLS_PER_ROW) * HEIGHT_OF_CELL) / 2)
return UIEdgeInsetsMake(inset, 0, inset, 0);
}
You will need to keep track of the total number of cells, number of cells on a row, and the cells height for the calculation to work right.
I am designing an app in which i have used a table view.This table view uses a custom cell.I have given proportinal height to the table view.T
tableview height constraints:
equal height to mainview
multiplier:189:568
Cell properties
cell height:77
Image constraints:
Label constraints
TextViewContsraints
bottom right label constraints
Code to make the row height dynamic
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath
{
float percentage = (77.0 / 568.0);
float height = self.view.frame.size.height * percentage;
return (height>77.0)?height:77.0;
}
Issue screen
Is the UITableViewCell in IB associated with a UITableViewCell subclass that overrides drawRect:? If so, make sure you are calling the super implementation, as that's what draws the line at the bottom. If you are overriding layoutSubviews make sure no views are obscuring the bottom of the cell.
In my case, i forgot to call [super layoutSubviews] after overriding the layoutSubviews method. Took me hoursto find the problem.
Hope this will help you.
Increase custom cell size to 80-88 may be it solve your problem
I'm trying to achieve such effect using Collection View :
UICollectionView image
Width of these cells should auto resize to width of the screen. Heights of each cell should be like 45% (10% is for this black gap). I tried a lot of combinations but I cannot achieve this in any way. Thank's in advance for any help.
Best regards,
Adrian.
Set the collectionView width to full screen. Implement these delegate method
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(collectionView.bounds.size.width, height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, bottom, 0);
}
I have attached an image here
I have taken a CollectionView Cell fitting the size of the width of the screen and half the size of the screen, taken a UIView and dropped it inside the collection view cell with a padding of 10-15 px from the bottom, clear the colour of the collection view background and that should give u the illusion of the CollectionViewCell cells having a padding to each other.
Whatever elements you want to show in the CollectionView Please add it to the view inside the view inside the CollectionViewCell cell
I am creating a UICollectionView which scrolls horizontally but with a vertical section header.
My problem is that for horizontal scrolling, the section header defaults to the left side of the collection view and I cannot adjust it to the top of the UICollectionView.
I have checked out various solutions which includes DateFlowLayout which unfortunately does not work anymore.
You have to implement your own collection view layout to do so.
The easiest would be to subclass UICollectionViewFlowLayout and override
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;
to give the frame you wish for the header