【问题标题】:how to make space on both side of the row uicollectionView如何在行uicollectionView的两侧腾出空间
【发布时间】:2018-01-11 05:11:34
【问题描述】:

我已经向超级视图添加了一个 uicollectionView 并像这样决定单元格大小:

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    if(!cell){
        cell = [[UICollectionViewCell alloc]init];
    }
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:cell.bounds];

    int r = arc4random_uniform(4);
    NSString *imageName = [[NSString stringWithFormat:@"%d", r] stringByAppendingString:@".jpg"];
    imageView.image = [UIImage imageNamed:imageName];
    [cell addSubview:imageView];
    cell.backgroundColor=[UIColor greenColor];
    return cell;
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(180, 340);
}
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1500;
}

在设备上看起来像这样:

我希望两个图像之间的空间以及每行的填充和尾部相等。

我还想设置每行之间的间距。

我该怎么做?

【问题讨论】:

    标签: ios objective-c uicollectionview


    【解决方案1】:

    func UIEdgeInsetsMake(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> UIEdgeInsets

    说明

    为按钮或视图创建边缘插图。

    插图是围绕 a 的边距 长方形。正值表示更靠近中心的边距 矩形,而负值表示距离更远的边距 中心。

    sectionInset 将在collectionview scetion 的Leading 和Trailing 上留出空间

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsetsMake(0, 4.0, 0, 4.0)
    
        self.collectionView = UICollectionView(frame: coll_frame, collectionViewLayout: layout)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.register(PreferenceCollectionViewCell.self, forCellWithReuseIdentifier: String(describing: PreferenceCollectionViewCell.self))
        self.addSubview(self.collectionView)
    

    这两种方法会给每个单元格留出空间,首先你可以传0.0,试试看,它是如何工作的。

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 6
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 6
    }
    

    【讨论】:

      【解决方案2】:

      从情节提要中,选择UICollectionView,转到属性并更改Inset。

      编辑 通过代码实现

      添加 UICollectionViewDelegateFlowLayout

      class SecondVC: UIViewController , UICollectionViewDataSource, UICollectionViewDelegate , UICollectionViewDelegateFlowLayout
      

      在下面实现这个函数

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
              return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
          }
      

      输出

      在 Obj-c 中

      使用这个函数

      - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView 
                              layout:(UICollectionViewLayout *)collectionViewLayout 
              insetForSectionAtIndex:(NSInteger)section;
      

      并返回UIEdgeInsetsMake(10, 10, 10, 10);

      【讨论】:

      • 感谢您的回答,但我没有使用故事板
      • @armnotstrong 检查编辑。
      【解决方案3】:

      试试这个

      通过 CGSizeMake 任何你想要的

      - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
      
      return CGSizeMake((self.view.frame.size.width/5) , (self.view.frame.size.width/5) - 25);
      }
      

      【讨论】:

        【解决方案4】:

        为单元格提供单元格间距和行间距。它根据提供的值调整单元格。

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 2011-08-19
          • 1970-01-01
          • 2015-07-03
          • 2021-02-05
          • 2022-01-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-01
          相关资源
          最近更新 更多