0

I am working on collection view in objective c,

My problem was

1.I want to change the cell size according to it's content size 2.If there are no image in cell then like and comment view should go above(refer image).enter image description here

I have changed the constraint like

 NSLayoutConstraint *newConstraint;

 if([image isEqualToString:@"no_image.jpg"] || [image isEqualToString:@"no_image.jpg"]){

    cell.desc_ImgViewWidth.constant = 0;

    [cell.Descimgview setHidden:YES];

    newConstraint = [NSLayoutConstraint constraintWithItem:(cell.bottomConstraint).firstItem attribute:(cell.bottomConstraint).firstAttribute relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:(cell.bottomConstraint).secondItem attribute:(cell.bottomConstraint).secondAttribute multiplier:(cell.bottomConstraint).multiplier constant:(cell.bottomConstraint).constant];

}
else{

    cell.desc_ImgViewWidth.constant = 120;
    [cell.Descimgview setHidden:NO];

    newConstraint = [NSLayoutConstraint constraintWithItem:(cell.bottomConstraint).firstItem attribute:(cell.bottomConstraint).firstAttribute relatedBy:NSLayoutRelationEqual toItem:(cell.bottomConstraint).secondItem attribute:(cell.bottomConstraint).secondAttribute multiplier:(cell.bottomConstraint).multiplier constant:(cell.bottomConstraint).constant];

}
    [cell.contentView removeConstraint:(cell.bottomConstraint)];
    [cell.contentView addConstraint:newConstraint];

    [cell layoutIfNeeded];
    [[cell contentView] setFrame:[cell bounds]];
    [[cell contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

in cellForItemAtIndexPath delegate method.(like and comment view moving above at first, but after reloading the cell again i.e, like scrolling etc the constraint is not working perfectly)

I want to move like and comment view like this and to reduce the cell height for that particular cell(refer below image)

enter image description here

How to properly do this?

Hira
  • 273
  • 3
  • 18
  • 1
    It's only a partial screenshot, but it seems to that a `UITableView` should be a better choice. Else, you see this: https://stackoverflow.com/questions/25895311/uicollectionview-self-sizing-cells-with-auto-layout ? – Larme Feb 22 '18 at 12:43
  • Use two cells one is with image and another one is with out image. But for self sizing the cell content the table view is the best choice. – phani Feb 22 '18 at 14:04

2 Answers2

2

You can use UICollectionViewLayout

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

         if imageView != nil
         {
            return CGSizeMake(width, height)
         }
         else
         {
            return CGSizeMake(width, height)
         }
    }
Kamlesh Shingarakhiya
  • 2,537
  • 1
  • 13
  • 34
0

Finally achieved like this,

 - (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
   sizeForItemAtIndexPath:(NSIndexPath *)indexPath
     {

       NSDictionary* object = [_Data objectAtIndex:indexPath.item];

       NSString *image = [object valueForKey:@"image"];

       if([image isEqualToString:@"no_image.jpg"] || [image isEqualToString:@"no_image.jpg"]){

          CGRect screenRect = [[UIScreen mainScreen] bounds];
          CGFloat screenWidth = screenRect.size.width;
          float cellWidth = screenWidth;

          NSDictionary* object = [_Data objectAtIndex:indexPath.item];


          NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[SwiftHelper getEmojiText:[object valueForKey:@"description"]]];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Georgia" size:15.0] range:NSMakeRange(0, str.length)];

          CGSize sizeName = CGRectIntegral([str boundingRectWithSize:CGSizeMake(cellWidth-8, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil]).size;

          NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:[SwiftHelper getEmojiText:[object valueForKey:@"name"]]];
    [str1 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Georgia" size:15.0] range:NSMakeRange(0, str1.length)];

          CGSize sizeName1 = CGRectIntegral([str1 boundingRectWithSize:CGSizeMake(cellWidth-8, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil]).size;
          NSLog(@"%f,%f",sizeName.height,sizeName1.height);
         if(sizeName.height > 100){
            sizeName.height = 70;
         }
         return CGSizeMake(cellWidth, sizeName.height + sizeName1.height + 110);


     }
     else{

         CGRect screenRect = [[UIScreen mainScreen] bounds];
         CGFloat screenWidth = screenRect.size.width;
         float cellWidth = screenWidth;

         CGSize size = CGSizeMake(cellWidth, 182);

         return size;
     }

Thank you for your suggestions friends.

Really it's better to use tableview for this but unfortunately after using collection view for a previous design the requirement changed like this. So, I struggled.

Hira
  • 273
  • 3
  • 18