3

In my UICollectionViewCell class I wrote this:

- (void)layoutSubviews {
    [super layoutSubviews];
    self.myImageView.layer.cornerRadius  = CGRectGetHeight(self.myImageView.frame) / 2;
    self.myImageView.layer.masksToBounds = YES;
}

But this does not work correctly in all cases, it appears like this:

enter image description here

Pagly
  • 61
  • 6
  • Works for me alright. Can't see anything wrong with your code. Try accessing width directly instead of using a getter for height. `self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2;`http://www.appcoda.com/ios-programming-circular-image-calayer/ – NSNoob Nov 16 '15 at 10:41
  • Also You are setting corner radius in `viewDidLayOutSubviews`. It is called multiple times for each subview being added. Dont put it there – NSNoob Nov 16 '15 at 10:43
  • 2
    For circle your height and width must be equal. And cornerRadius should be half of height or width. Add self. myImageView.clipsToBounds = YES; – Muhammad Adnan Nov 16 '15 at 10:46
  • Great catch Muhammad. – NSNoob Nov 16 '15 at 10:49
  • You are right, Good point Muhammad, but still it does not work – Pagly Nov 16 '15 at 11:14

1 Answers1

7

I faced a similar problem trying to get a collectionView inside a TableView Cell.

If you are changing your cell's model you should tell the cell to recalculate its layout so the cell changes its size and calls layoutIfNeeded.

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath
         UICollectionViewCell*cell = ...
         // Do your stuff here to configure the cell
         // Tell the cell to redraw its contentView        
         [cell layoutIfNeeded];
    }
Community
  • 1
  • 1
Pablo Romeu
  • 2,033
  • 1
  • 13
  • 15