0

的高度其實我要動態地設置按鈕和按鍵的標題出現動態我都試試這個,得到了動態內容的寬度,但不是在設置單元格的寬度和高度。一套動態的寬度和收集觀察室

- (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 look like this when i used this code

這是完美的我想如果我讓細胞活力的寬度和高度,所以請指導我這個。

+0

你想讓單元格寬度足夠大以顯示單元格文本嗎? –

+0

或單元格+標籤高度以顯示全文? –

+0

現全文細胞 – Virus

回答

2

編譯馬克 - 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.row]; 

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

     return CGSizeMake(sizeFromString, sizeFromString); 
    } 

您可能需要更改內標籤的高度也爲這款採用同樣的方法在你UICollectionViewcellForItemAtIndexPath方法。 此外,您可以自定義您自己的方式。

+0

它在工作謝謝 – Virus

+0

工作代碼。好東西 – pkc456

0

你可以把不同大小的橫向&肖像模式試試這個 -

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

可以調用invalidateLayout方法對你UICollectionView

- (void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 
    [yourCollectionView.collectionViewLayout invalidateLayout]; 
} 
+0

嘿這一個不工作,實際上我不得不返回CGSizeMake(rect.size.width,50); (UICollectionView *)collectionView layoutImage(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath – Virus

1

在雨燕2.0 .collectionViewLayout屬性:
使用一個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) 
    } 

}

+0

當您在Swift 3.0的collectionview中添加新行時,estimatedItemSize會發生布局問題。請檢查。 –

-1

你可以試試這個...

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

寬度,高度是動態計算的值

感謝