gpt4 book ai didi

ios - 以编程方式在 UICollectionViewController 的单元格内设置 UIView subview 的高度?

转载 作者:可可西里 更新时间:2023-11-01 04:23:30 25 4
gpt4 key购买 nike

在我的单元格的可重用 View 中,我有一个 UIView。

然后,我在 Controller 中有这个方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

// .. set value for CGFloat backgroundHeight

[cell addSubview:cell.backgroundView];

CGRect f = cell.backgroundView.frame;
f.size.height = backgroundHeight;
cell.backgroundView.frame = f;

}

但是 UIView 的高度与 Layout Rectangle 中指定的高度保持一致。

接下来我应该尝试什么?

最佳答案

这里的问题在于您试图使用单元格的 backgroundView

首先,您不能添加单元格的backgroundView 作为 subview 。您只需为它分配一个 UIView :

cell.backgroundView = yourView;

其次,如果您阅读 docs ,它明确指出:

Use this property to assign a custom background view to the cell. The background view is placed behind the content view and its frame is automatically adjusted so that it fills the bounds of the cell.

这意味着,无论您尝试为 backgroundView 设置什么 frame,它都会自动调整并填充整个单元格。现在,我还没有实际尝试过,但是您可能能够通过子类化来覆盖它。不过,我会在这里提到,我不确定。

回到你的问题,如果你真的想要一个你可以控制的UIView,你需要创建一个UIView,然后将它添加为一个 subview 。使用单元格的 backgroundView 不是解决方案。

关于ios - 以编程方式在 UICollectionViewController 的单元格内设置 UIView subview 的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22523446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com