Ios 在起始位置显示CollectionViewCell

Ios 在起始位置显示CollectionViewCell,ios,objective-c,xcode,uicollectionview,Ios,Objective C,Xcode,Uicollectionview,我用Xib文件创建了一个集合视图单元格。我只有一个单元格,但这个单元格显示在collectionView的中心。我想在collectionView的起始位置显示单元格 我修复了viewDidLoad中的contentInset,并在此处加载单元格xib文件 _listCollectionView.contentInset = UIEdgeInsetsMake(0 , 0, 0, 0); 我的手机大小是 - (CGSize)collectionView:(UICollectionView *

我用Xib文件创建了一个集合视图单元格。我只有一个单元格,但这个单元格显示在collectionView的中心。我想在collectionView的起始位置显示单元格

我修复了viewDidLoad中的contentInset,并在此处加载单元格xib文件

_listCollectionView.contentInset = UIEdgeInsetsMake(0 , 0, 0, 0);  
我的手机大小是

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


    return  CGSizeMake(156,140);
}
细胞显示像这样


我不太清楚你的问题,但我认为你指的是将collectionnView元素与视图对齐。您必须使用
UICollectionViewLayoutAttribute
layoutAttributesforElements
来实现这一点。看看下面的代码,如果你想在Objective c中使用它,就可以使用它

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *attributes = [super layoutAttributesForElementsInRect:rect];

    CGFloat leftMargin = self.sectionInset.left; //initalized to silence compiler, and actaully safer, but not planning to use.
    CGFloat maxY = -1.0f;

    //this loop assumes attributes are in IndexPath order

    for (UICollectionViewLayoutAttributes *attribute in attributes) {
    if (attribute.frame.origin.y >= maxY) {
        leftMargin = self.sectionInset.left;
    }


     attribute.frame = CGRectMake(leftMargin, attribute.frame.origin.y, 
     attribute.frame.size.width, attribute.frame.size.height);

     leftMargin += attribute.frame.size.width + self.minimumInteritemSpacing;
     maxY = MAX(CGRectGetMaxY(attribute.frame), maxY);
    }

     return attributes;

    }

collectionView:cellForItemAtIndexPath:
函数中,我做了如下更改

在这里,加载单元格后,我将collectionviewcellx原点位置固定为0

deviceCell= (DeviceCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"DeviceCollectionViewCell" forIndexPath:indexPath];

if (listArray.count == 1) {//Here i fixed the my cell x origin position
    CGRect frameRect = deviceCell.frame;//Copy cell frame
     frameRect.origin.x = 0;//Set cell origin.x
     deviceCell.frame = frameRect;//Reset frame
}

你能为同一个@Er添加附件吗。Khatri,我附上了一张图片,请检查一下。默认情况下,collectionView对齐是居中的,所以若你们只有一个单元格,那个么试着在视图上对齐collectionView。(与单元格大小相同的集合视图)并与视图左侧对齐@Pravin Tate,热修复。@正如Pravin所说,您可以将宽度和高度集合视图更改为单元格宽度和高度。您可以通过编程实现这一点。如果您使用的是自动布局,则将插座连接设置为“宽度”和“高度”,并更改其值。另一种方法是,可以使用瀑布模式进行集合视图流布局。有许多框架可用于此。你可以用它们,看看我的答案。这很简单,对我来说很有效。。。非常感谢你的回答。