0

在演示項目中,我可以按「添加新標籤」按鈕添加UICollectionViewCell標籤,其中包含我新輸入的文本。添加具有動態更改的單元寬度的UICollectionViewCell使minimumInteritemSpacing無效?

這是目標效果:

target effect

這是目前的情況: wrong effect

在當前狀態下,我重新配置小區UI在cellForItemAtIndexPath功能和調整在sizeForItemAtIndexPath功能單元尺寸。但是,這些操作似乎導致minimumInteritemSpacing不起作用。我使用UICollectionViewFlowLayout。

回答

0

我想你也可以繼承UICollectionViewFlowLayout,並覆蓋方法 -

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect

#import <UIKit/UIKit.h> 
@interface CustomViewFlowLayout : UICollectionViewFlowLayout 

@property(nonatomic) CGFloat maximumLineSpacing; 

@end 

@implementation CustomlViewFlowLayout 

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { 
    NSArray *answer = [super layoutAttributesForElementsInRect:rect]; 
    for(int i = 1; i < answer.count; ++i) { 
     UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i]; 
     UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1]; 

     NSInteger maximumSpacing = self.maximumLineSpacing; 
     NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame); 

     if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) { 
      CGRect frame = currentLayoutAttributes.frame; 
      frame.origin.x = origin + maximumSpacing; 
      currentLayoutAttributes.frame = frame; 
     } 
     if (currentLayoutAttributes.frame.origin.y != prevLayoutAttributes.frame.origin.y) { 
      CGRect frame = currentLayoutAttributes.frame; 
      frame.origin.x = 0; 
      currentLayoutAttributes.frame = frame; 
     } 
    } 
    return answer; 
} 

@end 

用來設置行的最大空間

財產 maximumLineSpacing
相關問題