相关疑难解决方法(0)

UICollectionViewCell中的自动布局无法正常工作

我有一个简单的UICollectionView,单元格有一个UITextView.UITextView受限于单元格的边缘,因此它们应保持与单元格大小相同的大小.

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

我在故事板中将单元格大小设置为320x50.如果我使用sizeForItemAtIndexPath:返回大小为单元格大小2倍的大小,则尽管我设置了约束,但UITextView仍保持相同的高度.我正在使用Xcode 6 GM.

我的视图控制器代码是:

@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; …
Run Code Online (Sandbox Code Playgroud)

ios autolayout uicollectionview uicollectionviewcell

51
推荐指数
3
解决办法
4万
查看次数