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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25804588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:24:15  来源:igfitidea点击:

Auto Layout in UICollectionViewCell not working

iosuicollectionviewautolayoutuicollectionviewcell

提问by ryanthon

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.

我有一个简单的 UICollectionView,其中包含具有单个 UITextView 的单元格。UITextView 被限制在单元格的边缘,所以它们应该保持与单元格大小相同的大小。

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:.

我遇到的问题是,由于某种原因,当我通过 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.

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

viewDidAppear 中的那些日志:给我一个输出:
100.00000
50.00000
如您所见,UITextView 高度不随单元格高度而变化。


Here's a screenshot of the storyboard I set up with a UITextView constrained in the UICollectionViewCell:


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

enter image description here

在此处输入图片说明

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?

我知道使用自动布局约束可以很好地与 UITableViewCells 和动态调整大小配合使用。我不知道为什么在这种情况下它不起作用。有没有人有任何想法?

回答by ryanthon

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:

好吧,我刚刚查看了 iOS 开发者论坛。显然,这是在 iOS 7 设备上运行的 iOS 8 SDK 的一个错误。解决方法是将以下内容添加到 UICollectionViewCell 的子类中:

- (void)setBounds:(CGRect)bounds {
    [super setBounds:bounds];
    self.contentView.frame = bounds;
}
override var bounds: CGRect {
    didSet {
      contentView.frame = bounds
    }
}

回答by Hamza

Equivalent Swift Code :

等效的 Swift 代码:

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

回答by itsji10dra

Here is the solution, if you are not subclassing UICollectionViewCell. Just add this two lines under your cellForItemAtIndexPath:after dequeueReusableCellWithReuseIdentifier:

这是解决方案,如果您没有继承UICollectionViewCell. 只需在您的cellForItemAtIndexPath:后添加这两行dequeueReusableCellWithReuseIdentifier:

Obj-C

对象-C

[[cell contentView] setFrame:[cell bounds]];
[[cell contentView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

Swift - 2.0

斯威夫特 - 2.0

cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

回答by Bill Chan

Swift 2.0:

斯威夫特 2.0:

cell.contentView.frame = cell.bounds
cell.contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]