【问题标题】:Auto Layout in UICollectionViewCell not workingUICollectionViewCell 中的自动布局不起作用
【发布时间】:2014-11-06 09:56:47
【问题描述】:

我有一个简单的 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;

    CGSize size = flowLayout.itemSize;

    size.height = size.height * 2;

    return size;
}

@end

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


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

我知道使用自动布局约束适用于 UITableViewCells 和动态调整大小。我不知道为什么它在这种情况下不起作用。有人有什么想法吗?

【问题讨论】:

    标签: ios uicollectionview autolayout uicollectionviewcell


    【解决方案1】:

    嗯,我刚刚查看了 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
        }
    }
    

    【讨论】:

    • 太棒了!你救了我的命!这个解决方案在安装 iOS 8 xcode 6 等后对我有用。我所有的 tableview 都没有适应 uicollectionview 单元格大小。谢谢你:)
    • 如果它不适合你试试这个:awkerFromNib(){ contentView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight }
    • 我在装有 iOS 8 的 iPad 上遇到了同样的问题,这个解决方案也解决了这个问题。谢谢!
    • +1 这也是 iOS 8 中的一个问题。我有一个在 iOS 7 中运行良好但在 iOS 8 中运行良好的集合视图单元格。这解决了它。
    • 完全忘记了这一点。谢谢...在 iOS 7 设备上运行的最新 iOS 8 SDK 仍然存在问题。
    【解决方案2】:

    等效的 Swift 代码:

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

    【讨论】:

    • 这个答案很有帮助,但只是对已接受答案的翻译。这可能就是对这个答案投反对票的人的想法。
    • 它真的帮助了我...谢谢
    • 谢谢,我正在使用带有自定义 UICollectionViewLayout 的 iOS 10,我在其中计算单元格大小。自动布局不起作用,这解决了它
    【解决方案3】:

    如果您不继承 UICollectionViewCell,这就是解决方案。只需在cellForItemAtIndexPath: 下的dequeueReusableCellWithReuseIdentifier: 之后添加这两行

    Obj-C

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

    斯威夫特 - 2.0

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

    【讨论】:

    • 对我来说,它无需从代码中添加第二行就可以工作
    • 如果你的应用支持多方向,那么你也需要第二行。 :-) @Teena nath Paul
    【解决方案4】:

    斯威夫特 2.0:

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

    【讨论】:

    • 将此添加到cellForItemAt并且没有任何改变:(
    猜你喜欢
    • 2016-11-30
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多