自动布局在UICollectionViewCell不工作
我有一个包含了一个的UITextView细胞的简单UICollectionView。所述的UITextView约束到小区的边缘,因此,它们应保持的大小与细胞大小相同。
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.
我遇到的问题是,由于某种原因,当我通过的CollectionView指定单元格的大小这些限制不工作:布局:sizeForItemAtIndexPath:
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:.
我在故事板设置为320x50的单元格大小。如果我返回一个大小与sizeForItemAtIndexPath单元尺寸的2倍的高度:,所述的UITextView停留尽管我设置约束相同的高度。我使用X code 6 GM。
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.
我的视图控制器code是:
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
在viewDidAppear这些日志:给我的输出:
结果
100.00000
结果
50.00000
结果
正如你所看到的,UITextView的高度不与细胞改变高度。
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.
结果
这是我设置了在UICollectionViewCell限制的UITextView中的故事板的截图:
Here's a screenshot of the storyboard I set up with a UITextView constrained in the UICollectionViewCell:
我知道使用自动布局约束的正常工作与UITableViewCells和动态调整。我不知道为什么它不是在这种情况下工作。没有人有任何想法?
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?
好吧,我只是看着在iOS开发者论坛。显然,这是在iOS 7设备iOS的8 SDK运行的错误。解决方法是将以下添加到您的UICollectionViewCell的子类:
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:
- (void)setBounds:(CGRect)bounds {
[super setBounds:bounds];
self.contentView.frame = bounds;
}