如何以编程方式设置 UICollectionViewCell 宽度和高度 [英] How to set UICollectionViewCell Width and Height programmatically
问题描述
我正在尝试实现一个 CollectionView
.当我使用自动布局时,我的单元格不会改变大小,但会改变它们的对齐方式.
现在我宁愿将它们的大小更改为例如
//var size = CGSize(width: self.view.frame.width/10, height: self.view.frame.width/10)
我尝试在我的 CellForItemAtIndexPath
collectionCell.size = 大小
虽然没用.
有没有办法做到这一点?
编辑:
看来,答案只会改变我的 CollectionView 宽度和高度本身.约束可能存在冲突吗?对此有何想法?
使用此方法设置自定义单元格高度宽度.
确保添加此协议
UICollectionViewDelegateUICollectionViewDataSourceUICollectionViewDelegateFlowLayout
如果您使用的是 swift 5 或 xcode 11 及更高版本,则需要将 Estimate Size
设置为 none
使用故事板以使其正常工作.如果您不设置它,那么下面的代码将无法按预期工作.
Swift 4 或更高版本
extension YourViewController: UICollectionViewDelegate {//在这里写委托代码}扩展 YourViewController: UICollectionViewDataSource {//这里写数据源代码}扩展 YourViewController: UICollectionViewDelegateFlowLayout {func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) ->CG尺寸{return CGSize(width: screenWidth, height: screenWidth)}}
<块引用>
目标 C
@interface YourViewController : UIViewController- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{返回 CGSizeMake(CGRectGetWidth(collectionView.frame), (CGRectGetHeight(collectionView.frame)));}
I am trying to implement a CollectionView
.
When I am using Autolayout, my cells won't change the size, but their alignment.
Now I would rather want to change their sizes to e.g.
//var size = CGSize(width: self.view.frame.width/10, height: self.view.frame.width/10)
I tried setting in my CellForItemAtIndexPath
collectionCell.size = size
it didn't work though.
Is there a way to achieve this?
edit:
It seems, that the answers will only change my CollectionView width and height itself. Is there are conflict in Constraints possible? Any ideas on that ?
Use this method to set custom cell height width.
Make sure to add this protocols
UICollectionViewDelegate
UICollectionViewDataSource
UICollectionViewDelegateFlowLayout
If you are using swift 5 or xcode 11 and later you need to set Estimate Size
to none
using storyboard in order to make it work properly. If you will not set that than below code will not work as expected.
Swift 4 or Later
extension YourViewController: UICollectionViewDelegate {
//Write Delegate Code Here
}
extension YourViewController: UICollectionViewDataSource {
//Write DataSource Code Here
}
extension YourViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: screenWidth, height: screenWidth)
}
}
Objective-C
@interface YourViewController : UIViewController<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(CGRectGetWidth(collectionView.frame), (CGRectGetHeight(collectionView.frame)));
}
这篇关于如何以编程方式设置 UICollectionViewCell 宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!