简体   繁体   English

如何在单元格之间添加边距-启用UICollectionView分页

[英]How to add a margin between the cells - UICollectionView paging enabled

I have a UICollectionView with paging enabled. 我有一个启用了分页的UICollectionView。 Each cell takes up the size of the screen. 每个单元格占用屏幕的大小。 It works similar to an image gallery, however, I would like to add 5px of padding between my cells. 它的工作原理类似于图片库,但是,我想在单元格之间添加5px的填充。 When I do this in InterfaceBuilder it affects the alignment of my cells (the first cell its the screen, the second cell ends 5pxs in shower the end of the first cells etc). 当我在InterfaceBuilder执行此操作时,它会影响我的单元格的对齐方式(第一个单元格在屏幕上,第二个单元格在第一个单元格的末尾以5pxs的间隔结束)。

I've been pulling my hair out all morning. 我整个上午都在梳头。 Here is my code; 这是我的代码;

.m .m

@interface GCViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic) CGFloat minimumLineSpacing;
@property (nonatomic) UIEdgeInsets sectionInset;

@property (nonatomic)NSMutableArray * collArray;
@end

@implementation GCViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.collectionView.delegate =self;
    self.collectionView.dataSource=self;
    self.collArray = [[NSMutableArray alloc] initWithObjects:[UIColor greenColor],[UIColor redColor],[UIColor blueColor],[UIColor purpleColor],[UIColor greenColor], nil];

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGSize size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);

    return size;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.collArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [self.collArray objectAtIndex:indexPath.row];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

在此处输入图片说明在此处输入图片说明

I use the following simple solution to insert a margin between every item (like UIScrollView's preformance): Expand every collectionViewItem's width by a fixed value 'pageSpacing' (such as 10.0), and expand the collectionView's width by the same value, too. 我使用以下简单解决方案在每个项目之间插入边距(例如UIScrollView的性能):将每个collectionViewItem的宽度扩展为固定值'pageSpacing'(例如10.0),并将collectionView的宽度扩展为相同值。 And don't forget that, when layout collevtionViewCell's subviews, there's an additional spacing which is not for diplaying the real content. 并且不要忘记,当布局collevtionViewCell的子视图时,存在一个额外的间距,该间距不能用于显示实际内容。

Heres the code written in Swift 3.1, I'm sure it's easily understanding for you: 这是用Swift 3.1编写的代码,我相信它很容易为您理解:

 let pageSpacing: CGFloat = 10

 class ViewController: UITableViewController {
      override func viewDidLoad() {
         super.viewDidLoad()

         let layout = UICollectionViewFlowLayout()
         layout.itemSize                = CGSize(width: view.frame.width + pageSpacing, height: view.frame.height)
         layout.scrollDirection         = .horizontal
         layout.minimumInteritemSpacing = 0
         layout.minimumLineSpacing      = 0

         let frame = CGRect(x: 0, y: 0, width: view.frame.width + pageSpacing, height: view.frame.height)
         let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
         view.addSubview(collectionView)
     }
 }

 class MyCell: UICollectionViewCell {
     var fullScreenImageView = UIImageView()
     override func layoutSubviews() {
         super.layoutSubviews()
         fullScreenImageView.frame = CGRect(x: 0, y: 0, width: frame.width - pageSpacing, height: frame.height)
     }
 }

Hope that can help you. 希望能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM