I've an array of UIColor's in my data source, what I'm trying to do is to fit all cells' size so they can all be seen on screen without scrolling.
I don't know how to do it, so far I've:
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
UIColor *cellColor = (UIColor *)self.dataSource[indexPath.row];
cell.backgroundColor = cellColor;
for (UIView *view in cell.contentView.subviews) {
if ([view isKindOfClass:[UIImageView class]]) {
if ([[(UIImageView *)view image] isEqual:[UIImage imageNamed:#"V_BG.png"]]) {
[view removeFromSuperview];
}
}
}
if ([cellColor isEqual:self.existingColor]) {
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"V_BG.png"]];
img.frame = CGRectMake(0, 0, 30, 30);
img.center = cell.contentView.center;
img.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:img];
}
return cell;
}
#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[[NSNotificationCenter defaultCenter] postNotificationName:UIColorDidChangeNotification
object:[NSString stringWithFormat:#"%ld", (long)indexPath.row]];
[self.navigationController popViewControllerRetro];
}
#pragma mark Collection view layout things
// Layout: Set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat fixSize = ([UIScreen mainScreen].bounds.size.width / 3) - 2;
return CGSizeMake(fixSize, fixSize);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 2;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 2;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
Related
I'm Creating Custom UICollectionViewCell it's size automatically set from it's Content.
The Dynamic Size of cell is Working But In That It Have Extra Space.
So I Want to remove extra Space Between Two Cell
I'm also set UICollectionView DataSource and Delegate and Impalement It's Method But Still it is not Working
In One Row not fix their only 2 cell. It have 1, 2, or 3 cell it's depend on Text
CustomeCollectionViewCell.m
- (CGSize)intrinsicContentSize
{
CGSize size = self.lblText.intrinsicContentSize;
size.width += 48; // add padding Width that Given in Cell
size.height += 32; // add padding Height
return size;
}
ViewController.m
#interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
#property (strong, nonatomic) CustomeCollectionViewCell *tempCell;
#property (strong, nonatomic) NSArray *dataArr;
#end
- (void)viewDidLoad {
[super viewDidLoad];
self.dataArr = #[#"abcdef”,#"abcdef”,#"abcdef”,#"abcdef”,#"abcdef”];
self.tempCell = (CustomeCollectionViewCell*) [[[UINib nibWithNibName:#“CustomeCollectionViewCell" bundle:nil] instantiateWithOwner:self options:nil] firstObject];
[self.collectionView setContentInset:UIEdgeInsetsMake(8, 8, 8, 8)];
[self.collectionView registerNib:[UINib nibWithNibName:#"CustomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"CustomeCollectionViewCell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CustomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CustomeCollectionViewCell" forIndexPath:indexPath];
cell.lblText.text = [self.dataArr objectAtIndex:indexPath.item];
cell.backgroundColor = [UIColor blackColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
self.tempCell.lblText.text = [self.dataArr objectAtIndex:indexPath.item];
return self.tempCell.intrinsicContentSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 8;
}
Try to add Dynamic Size of cell with following delegate method
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake((self.view.frame.size.width - 15)/2, (self.view.frame.size.width - 15)/2);
}
I have an UICollection View i am using the peek and pop method to look into it normally it works fine but if i use peek and pop i get the error
The code for the collection view class
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
AppsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
cell.titleLabel.text = #"TwitClient";
cell.iconimg.image = [UIImage imageNamed:#"twitclient"];
cell.appsbutton.userInteractionEnabled = NO;
cell.appsbutton.hidden = NO;
cell.appsbutton.alpha = 0.0;
cell.phoneImageView.image = [UIImage imageNamed:#"app3"];
cell.descLabel.text = #"TwitClient is an app which helps you find tweets on any subject from around the earth. You can search tweets with or without hashtags";
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(69, 126, 277, 490)];
imageView.image = [UIImage imageNamed:#"T-2"];
[cell addSubview:imageView];
return cell;
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
This is where 3d touch is implmented
- (UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
AppsViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"...."];
previewingContext.sourceRect = self.appsView.frame;
return appsvc;
}
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self presentViewController:viewControllerToCommit animated:YES completion:nil];
}
I dont really know what i have done wrong i have done another collection View like this but no error's
I have a UICollectionView which should have 3 columns and 5 rows. They are static and won't be changed. the UICollectionView should not scroll, so the cells are visible the whole time. It looks good in portrait mode, but the view is messed up in landscape mode. How to reorder cells in meaningful way? or is it possible to stretch the content so that all cells are visible?
portrait:
landscape:
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
UICollectionView *collec = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 160, 320, 280) collectionViewLayout:layout];
[collec registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"test"];
collec.backgroundColor = [UIColor yellowColor];
collec.delegate = self;
collec.dataSource = self;
collec.scrollEnabled = NO;
collec.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:collec];
NSDictionary *headerViewDic = #{#"collec" : collec};
NSArray *constraintsHeaderView = [NSLayoutConstraint constraintsWithVisualFormat:#"|[collec]|" options:0 metrics:nil views:headerViewDic];
NSArray *constraintsHeaderViewV = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-160-[collec]-8-|" options:0 metrics:nil views:headerViewDic];
[self.view addConstraints:constraintsHeaderView];
[self.view addConstraints:constraintsHeaderViewV];
[collec setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal | UILayoutConstraintAxisVertical ];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 3;
}
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"test" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize mElementSize = CGSizeMake(97, 50);
return mElementSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0,7,7,7); // top, left, bottom, right
}
Try to find the device orientation and then swap your section and row values so that your row take place
I want to create 3 cells in each row, with width & height 104.0f each. But I am getting the output as shown in the screenshot.
The code used is given below. Since I am new to UICollectionView, I have no idea how to implement this. Where do I design the cells incase if frames are different for all?
- (void)viewDidLoad
{
....
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
dealListCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(1.0f, 1.0f, 320.0f, 525.0f) collectionViewLayout:layout];
[dealListCollectionView setDataSource:self];
[dealListCollectionView setDelegate:self];
[dealListCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[dealListCollectionView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:dealListCollectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor greenColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(104, 104);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (UIEdgeInsets)insetsForItemAtIndexPath:(NSIndexPath *)indexPath {
return UIEdgeInsetsMake(2, 2, 2, 2);
}
Click on your CollectionView, Go to the Size Inspector and change the default value of Min Spacing For Cells and For Rows. For your size 104x104 it have to be 4px.
The below code solved my problem :
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 2.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 2.0;
}
I have horizontal scrolling set up now I would like for it to sort of scroll/snap one cell at a time when scrolling side to side.
Could someone shed some light? Here's the code I have so far:
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}
[collectionView setPagingEnabled:YES]; might do the trick?
http://adoptioncurve.net/archives/2013/04/creating-a-paged-photo-gallery-with-a-uicollectionview/