UICollectionViewCell 中的自动布局不起作用 [英] Auto Layout in UICollectionViewCell not working

查看:30
本文介绍了UICollectionViewCell 中的自动布局不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 UICollectionView,其中的单元格只有一个 UITextView.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:layout: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:.

我在 Storyboard 中将单元格大小设置为 320x50.如果我使用 sizeForItemAtIndexPath: 返回单元格高度的 2 倍的大小,则尽管我设置了约束,UITextView 仍保持相同的高度.我使用的是 Xcode 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.

我的视图控制器代码是:

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;
}

override var bounds: CGRect {
    didSet {
      contentView.frame = bounds
    }
}

这篇关于UICollectionViewCell 中的自动布局不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆