首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UICollectionViewCell中的自动布局不起作用

UICollectionViewCell中的自动布局不起作用
EN

Stack Overflow用户
提问于 2014-09-12 17:06:35
回答 4查看 39.6K关注 0票数 52

我有一个简单的UICollectionView,它的单元格只有一个UITextView。UITextView被约束到单元格的边缘,因此它们应该保持与单元格大小相同的大小。

我遇到的问题是,由于某些原因,当我通过collectionView:layout:sizeForItemAtIndexPath:.指定像元大小时,这些约束不起作用

我在故事板中将单元大小设置为320x50。如果我用sizeForItemAtIndexPath:返回一个高度是单元格高度的2倍的大小,尽管我设置了约束,UITextView仍然保持相同的高度。我使用的是Xcode6 GM。

我的视图控制器代码是:

代码语言:javascript
复制
@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高度不会随单元格高度而改变。

以下是我在UICollectionViewCell中使用约束的UITextView设置的故事板的屏幕截图:

我知道使用自动布局约束可以很好地处理UITableViewCells和动态调整大小。我不知道为什么它在这种情况下不起作用。有谁有什么想法吗?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25804588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档