2014-09-12 09:06:34 8 Comments
I have a simple UICollectionView with cells that have a single UITextView. The UITextView is constrained to the edges of the cell, so they should stay the same size as the cell size.
The problem I'm having is that for some reason these constraints are not working when I specify the cell size via collectionView:layout:sizeForItemAtIndexPath:.
I have the cell size set to 320x50 in the Storyboard. If I return a size with 2 times the height of the cell size with sizeForItemAtIndexPath:, the UITextView stays the same height despite the constraints I set. I am using Xcode 6 GM.
My view controller code is:
@implementation TestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UICollectionViewCell *c = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
NSLog(@"%f", c.frame.size.height);
UITextView *tv = (UITextView *)[c viewWithTag:9];
NSLog(@"%f", tv.frame.size.height);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;
CGSize size = flowLayout.itemSize;
size.height = size.height * 2;
return size;
}
@end
Those logs in viewDidAppear: give me an output of:
100.00000
50.00000
As you can see, the UITextView height doesn't change with the cell height.
Here's a screenshot of the storyboard I set up with a UITextView constrained in the UICollectionViewCell:
I know using auto layout constraints works fine with UITableViewCells and dynamic resizing. I have no idea why it's not working in this case. Does anyone have any ideas?
Related Questions
Sponsored Content
25 Answered Questions
[SOLVED] Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
- 2013-09-11 16:48:39
- smileyborg
- 509910 View
- 1406 Score
- 25 Answer
- Tags: ios uitableview autolayout nsautolayout row-height
2 Answered Questions
Adding UICollectionViewController to UIViewController Not Working
- 2015-11-24 10:52:49
- ORStudios
- 760 View
- 1 Score
- 2 Answer
- Tags: ios objective-c uiview uicollectionview
1 Answered Questions
[SOLVED] UICollectionViewCell size zero in storyboard
- 2016-11-25 15:27:28
- andyPaul
- 507 View
- 0 Score
- 1 Answer
- Tags: uicollectionview uicollectionviewcell uicollectionviewlayout xcode8.1
2 Answered Questions
[SOLVED] Resizing UICollectionViewCell After Data Is Loaded Using invalidateLayout
- 2014-05-18 22:45:29
- Stringer Bell
- 11842 View
- 8 Score
- 2 Answer
- Tags: uicollectionview uicollectionviewcell uicollectionviewlayout
1 Answered Questions
[SOLVED] My CollectionViewCell is getting into a mess
- 2016-07-04 16:19:31
- Donghee Seo
- 310 View
- 0 Score
- 1 Answer
- Tags: ios swift uicollectionview uicollectionviewcell
1 Answered Questions
[SOLVED] UIImageView in UICollectionViewCell in UITableViewCell bug
- 2013-09-23 19:25:40
- aug2uag
- 3729 View
- 2 Score
- 1 Answer
- Tags: objective-c uitableview uiimageview uicollectionviewcell
1 Answered Questions
[SOLVED] UICollectionView not the right size in viewDidLoad
- 2015-09-14 21:00:03
- sebastien
- 754 View
- 2 Score
- 1 Answer
- Tags: ios autolayout uicollectionview viewdidload viewdidappear
2 Answered Questions
how to set dynamic uicollectionviewcell size from its content - programmatically
- 2013-12-17 17:27:37
- Pedroinpeace
- 2007 View
- 3 Score
- 2 Answer
- Tags: ios objective-c uicollectionview uicollectionviewcell contentsize
1 Answered Questions
[SOLVED] Sizing a UICollectionViewCell that contains UITextView to fit the contentSize
- 2013-07-15 19:28:25
- John Noonan
- 2596 View
- 8 Score
- 1 Answer
- Tags: ios cocoa-touch uitextview uicollectionview uicollectionviewcell
3 Answered Questions
[SOLVED] How can I optimize loading images from URL for uicollectionview?
- 2013-08-28 04:07:18
- East Bay Ray
- 3491 View
- 1 Score
- 3 Answer
- Tags: iphone ios objective-c uicollectionview
4 comments
@itsji10dra 2014-12-16 08:42:02
Here is the solution, if you are not subclassing
UICollectionViewCell
. Just add this two lines under yourcellForItemAtIndexPath:
afterdequeueReusableCellWithReuseIdentifier:
Obj-C
Swift - 2.0
@Teena nath Paul 2015-10-04 11:50:57
For me it worked without adding second line from the code
@itsji10dra 2015-10-05 07:43:07
If your app suports multiple orientation, then u will need second line also. :-) @Teena nath Paul
@Bill Chan 2015-09-25 21:05:36
Swift 2.0:
@ixany 2016-11-05 09:20:42
Added this to
cellForItemAt
and nothing changed :(@Hamza 2015-01-21 14:22:37
Equivalent Swift Code :
@cbh2000 2015-06-19 15:09:52
This answer is helpful, but is simply a translation of the accepted answer. That's probably what the person who down voted this answer thought.
@Vijay Sanghavi 2016-08-24 08:00:17
It really helped me...Thanks
@iWheelBuy 2017-03-04 09:03:45
It helped me also!!! Thanks
@xxtesaxx 2017-04-29 02:48:06
Thanks, I'm using iOS 10 with a custom UICollectionViewLayout where I calculate the cell size. Autolayout wasn't working and this fixed it
@ryanthon 2014-09-13 05:38:52
Well, I just looked on the iOS developer forums. Apparently this is a bug with the iOS 8 SDK running on iOS 7 devices. The workaround is to add the following to your subclass of UICollectionViewCell:
@Fjohn 2014-09-25 09:38:42
Great ! you save my day ! this solution working for me after installing iOS 8 xcode 6 etc. all my tableview didn't adapt the uicollectionview cell size. Thank you :)
@Alexander Polovinka 2014-11-07 21:27:21
in case it didn't work for you try this: awakerFromNib(){ contentView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight }
@almas 2015-05-04 19:37:40
I had same issue on iPad with iOS 8, and this solution fixed the problem as well. Thanks!
@ChrisH 2015-05-07 21:08:13
+1 on this being an issue in iOS 8 too. I had a collection view cell that was working fine in iOS 7, but not in iOS 8. This fixed it.
@Ari Braginsky 2015-06-10 11:32:07
Totally forgot about this. Thanks...still an issue with latest iOS 8 SDK running on iOS 7 devices.
@CAMOBAP 2015-12-29 15:51:47
I have faced with the same issue on iOS 9, Does it still not fixed?