设置集合视图单元格的dynamic宽度和高度

其实我想dynamic设置button和button的标题出现dynamic我已经尝试这个,并获得dynamic内容的宽度,但不是设置单元格的宽度和高度。

- (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); } 

和输出是这样的

当我使用这个代码时,OUTPUT看起来像这样

这是完美的,我认为如果我使细胞的dynamic宽度和高度,所以请指导我通过这个。

编译标记 – 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; } 

从这个方法获得string的高度,并将其添加到您的单元格或标签或button的任何想要使用的原始高度。

例如:

  - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { YourCellClass *myCell = (YourCellClass*) [collectionView cellForItemAtIndexPath:indexPath.row]; CGFloat sizeFromString = [self getTextHeightFromString:itemObject.MenuDescription ViewWidth:myCell.frame.size.width WithPading:10 FontName:@"HelveticaNeue" AndFontSize:13]; return CGSizeMake(sizeFromString, sizeFromString); } 

您可能还需要更改内部标签的高度,以便在您的UICollectionView cellForItemAtIndexPath方法中使用相同的方法。 进一步你可以定制你自己的方式。

在Swift 2.0中:
使用UICollectionViewFlowLayout启用单元格自动resize。
UICollectionViewController子类中的以下代码将启用其stream布局的自动resize。

 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 { // 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]; } 

你可以试试这个…

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

宽度,高度是dynamic计算的值

谢谢