关于collectionview布局的坑
不知道寫了多少次collectionview,步了很多坑,現(xiàn)在看來雖然達到了自己想要的結(jié)果,卻不知道其中所以然。還是總結(jié)一下,免得再走彎路;
場景是這樣的,我要定制一個顯示選擇圖片的排列,想要實現(xiàn)橫向排列4個,等間距,多了折行顯示的效果,正確的做法是這樣的;
- (void)viewDidLoad {[super viewDidLoad];self.navigationController.navigationBar.translucent = NO;if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {self.automaticallyAdjustsScrollViewInsets = NO;}self.view.backgroundColor = [UIColor purpleColor];self.photoArray = @[[UIImage imageNamed:@"cycle3"],[UIImage imageNamed:@"cycle4"],[UIImage imageNamed:@"cycle2"],[UIImage imageNamed:@"cycle3"],[UIImage imageNamed:@"cycle4"],].mutableCopy;[self.view addSubview:self.pickPhotoCollection]; }-(UICollectionView *)pickPhotoCollection{if (!_pickPhotoCollection) {UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];layout.scrollDirection = UICollectionViewScrollDirectionVertical;_pickPhotoCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 200, kScreenWidth, (self.photoArray.count/4 +1)*80) collectionViewLayout:layout];_pickPhotoCollection.delegate = self;_pickPhotoCollection.dataSource = self;_pickPhotoCollection.backgroundColor = RGBACOLOR(240, 240, 240, 1);_pickPhotoCollection.showsHorizontalScrollIndicator = NO;[_pickPhotoCollection registerClass:[XJPickPhotoCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XJPickPhotoCollectionViewCell class])];}return _pickPhotoCollection; } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{return 1; }-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{return self.photoArray.count + 1 > 9 ? 9 : self.photoArray.count +1; }-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{XJPickPhotoCollectionViewCell *cell = (XJPickPhotoCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XJPickPhotoCollectionViewCell class]) forIndexPath:indexPath];//判斷圖片的顯示方式 如果不是9張圖 則顯示可以繼續(xù)添加if (self.photoArray.count == 9) {cell.photoImageView.image = self.photoArray[indexPath.row];return cell;}if (indexPath.row == self.photoArray.count) {cell.photoImageView.image = [UIImage imageNamed:@"addPhoto"];}else{cell.photoImageView.image = self.photoArray[indexPath.row];}return cell; }-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{return CGSizeMake(WidthScale(70), WidthScale(70)); } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ // return 2;return (kScreen_width - WidthScale(70)*4 - 10 )/3.; } //-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ // return (kScreen_width - WidthScale(70)*4 - 10 )/3.; //} -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{return UIEdgeInsetsMake(5, 5, 5, 5); }關(guān)鍵點在這里:
1-===layout.scrolldirection = uicollectionViewScrollDirectionVertical;collection的滾動方向,由于要折行向下,所以方向是垂直方向,在排列的時候會默認選擇先將橫向排列完畢,折行向下。
2--=== minmumlinespaceing 這個這個屬性用來指示行與行之間的最小距離(縱向),或者列與列之間的最小距離(橫向)。無論橫向或者縱向,都可以滾動顯示所有內(nèi)容,所以這個屬性可以單獨設(shè)置。 ?SO 在不同的滾動方向上要分清楚他的作用,我就是在這里弄的頭暈?zāi)垦?#xff1b; ?當(dāng)然interitem就很好理解了 ?
?
另外還有一點是 ? 如果是自定義的layout,layout的代理方法是不會執(zhí)行的,若想達到相同的效果,需要在自定義的layout內(nèi)部進行處理!
? ? ? ? layout.scrollDirection = UICollectionViewScrollDirectionVertical;
?
轉(zhuǎn)載于:https://www.cnblogs.com/lidarui/p/6931415.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的关于collectionview布局的坑的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 道路减速带设置规范(道路减速带国家标准)
- 下一篇: git冲突Please move or