设置 collection 视图单元格的动态宽度和高度

set Dynamic width and height of collection view cell

实际上我想动态设置按钮并且按钮的标题显示为动态我已经尝试过并获得动态内容的宽度但我没有设置单元格宽度和高度。

- (void)viewDidLoad {
    [super viewDidLoad];

    ContentArray = [[NSMutableArray alloc]init];

    self.collectionview.delegate = self;
    self.collectionview.dataSource =self;
    self.collectionview.backgroundColor = [UIColor clearColor];


    [ContentArray addObject:@"Planning"];
    [ContentArray addObject:@"Resources"];
    [ContentArray addObject:@"Human Resources"];
    [ContentArray addObject:@"Agriculture"];
    [ContentArray addObject:@"Resources management"];


}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return ContentArray.count;
    }


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

        CollectionViewCell * Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell"forIndexPath:indexPath];

        [Cell.button setTitle:[ContentArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];

        return Cell;
    }


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {


        CGRect rect = [[ContentArray objectAtIndex:indexPath.row ] boundingRectWithSize:CGSizeMake(HUGE_VALF, 50) options:NSStringDrawingUsesFontLeading attributes:nil context:nil] ;


        return CGSizeMake(rect.size.width, 50);
    }

输出是这样的

如果我让单元格动态宽度和高度,我认为这是完美的,所以请指导我完成这个。

您可以在横向和纵向模式下设置不同的尺寸,试试这个 -

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    // Adjust cell size for orientation
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        return CGSizeMake(rect.size.width, 50);
    }
    return CGSizeMake(rect.size.width, 50);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.collectionView performBatchUpdates:nil completion:nil];
}

您可以在 UICollectionView

.collectionViewLayout 属性 上调用 invalidateLayout 方法
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [yourCollectionView.collectionViewLayout invalidateLayout];
}

在 Swift 2.0 中:
使用 UICollectionViewFlowLayout 启用单元格自动调整大小。
UICollectionViewController 子类中的以下代码将为其流布局启用自动调整大小。

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib. 
    if let cvl = collectionViewLayout as? UICollectionViewFlowLayout {
        cvl.estimatedItemSize = CGSize(width: 150, height: 75)
    }

}

你可以试试这个...

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(width, height);
}

width,height为动态计算值

谢谢

编译指示标记 - GetHeightFromString

- (CGFloat)getTextHeightFromString:(NSString *)text ViewWidth:(CGFloat)width WithPading:(CGFloat)pading FontName:(NSString *)fontName AndFontSize:(CGFloat)fontSize
{
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:fontName size:fontSize]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:attributes
                                     context:nil];
    //NSLog(@"rect.size.height: %f", rect.size.height);
    return rect.size.height + pading;
}

通过此方法获取字符串的高度并将其添加到您想要使用的任何单元格或标签或按钮的原始高度。

例如:

  - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        YourCellClass *myCell =  (YourCellClass*) [collectionView cellForItemAtIndexPath:indexPath];  

        CGFloat  sizeFromString   = [self getTextHeightFromString:itemObject.MenuDescription ViewWidth:myCell.frame.size.width WithPading:10 FontName:@"HelveticaNeue" AndFontSize:13];
    
        return CGSizeMake(sizeFromString, sizeFromString);
    }

您可能还需要更改内部标签的高度,为此请使用您 UICollectionViewcellForItemAtIndexPath 方法中的相同方法。 此外,您还可以自定义自己的方式。