collectionViewLayout sizeForItemAtIndexPath - > CGSize奇怪的行为

时间:2016-11-17 11:31:15

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

有趣的是,我尝试使用collectionViewLayout sizeForItemAtIndexPathcollectionView中一次只显示一个项目并横向滚动,我试过这个:

- (void)viewDidLoad {
    [super viewDidLoad];

    flowLayout = [[UICollectionViewFlowLayout alloc]init];

    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0.0;

    flowLayout.minimumLineSpacing = 0.0;

    _obj_CollectionView.pagingEnabled = YES;
    _obj_CollectionView.collectionViewLayout = flowLayout;

    self.obj_CollectionView.delegate = self;
    self.obj_CollectionView.dataSource = self;
    _obj_CollectionView.backgroundColor = [UIColor redColor];
}


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

这是collectionView截图:

enter image description here

这里的紫色是我的细胞,红色是我的collectionView,但我想要整个紫色。

所以请指导我。我很迷惑。 :)

6 个答案:

答案 0 :(得分:2)

首先,您不需要以编程方式创建UICollectionViewFlowLayout对象。也不需要用viewDidLoad()编写的代码。您可以使用storyboarddelegate方法完成所有这些操作。

以下是您的问题的代码:

import UIKit

class ViewController: UIViewController
{
    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }
}

extension ViewController : UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
        cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.purple : UIColor.blue
        return cell
    }
}

extension ViewController : UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)
    }

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

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

故事板截图:

enter image description here

输出屏幕:

enter image description here

答案 1 :(得分:1)

试试这个:

func collectionView(collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                           minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 1.0
}

func collectionView(collectionView: UICollectionView, layout
    collectionViewLayout: UICollectionViewLayout,
    minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 1.0
}

viewDidLoad中:

layout.minimumInteritemSpacing = 20
layout.minimumLineSpacing = 10

的cellForRowAtIndexPath:

cell.image.clipsToBounds = true
cell.contentView.frame = cell.bounds

答案 2 :(得分:0)

我做了一个概念验证,它对我来说是预期的。 你的CollectionViewCell(错误/缺失约束)可能有问题吗?

import UIKit


class CollectionViewController: UICollectionViewController {
    let layout = UICollectionViewFlowLayout()

    override func viewDidLoad() {
        super.viewDidLoad()

        layout.scrollDirection = .horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0

        collectionView?.collectionViewLayout = layout
        collectionView?.isPagingEnabled = true
    }


    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }


    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
        cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.purple : UIColor.blue

        return cell
    }
}

extension CollectionViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return self.view.frame.size
    }
}

答案 3 :(得分:0)

您有一些调试选项可以检查原因:

    {li> NSLog() UICollectionView 的框架
  • NSLog() CGSize检查实际返回的尺寸
  • 尝试设置硬编码大小以检查行为。
  • 如果使用autoLayout,请在删除约束后尝试。

希望这会有所帮助。

答案 4 :(得分:0)

好的,我明白了。 让我先发布我的故事板的布局。

我采用了iPhone6布局,并对集合视图提供了以下约束

设置布局

  1. 将集合视图的宽度修改为视图的宽度

    Image 1

  2. 下一步我修复了集合视图的宽高比,这样它总是一个正方形

  3. Image 2

    1. 最后我们添加了top,trailing和leading约束。
    2. Image3

      现在,我将布局从垂直方向更改为水平滚动,并添加了以下代码。

      代码实施

       #import "ViewController.h"
      
       @interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
      
       @property (nonatomic,weak) IBOutlet UICollectionView *clcnView;
      
       @end
      
       @implementation ViewController
      
      - (void)viewDidLoad {
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
      }
      
      #pragma mark - Collection View methods- 
      
       -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
           static NSString *reuse = @"cell";
           UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
           cell.backgroundColor = randomColor();
           return cell;
       }
      
      
      -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
           return 10;
       }
      
      
       - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
      
           CGFloat fl = self.clcnView.frame.size.width;
           CGSize mElementSize = CGSizeMake(fl-1, fl-1);
           return mElementSize;
       }
       - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
           return 1.0;
       }
      
         - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout  minimumLineSpacingForSectionAtIndex:(NSInteger)section {
           return 1.0;
       }
      
        - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
           return UIEdgeInsetsMake(0,0,0,0);
       }
      
      
       UIColor * randomColor(){
           return [UIColor colorWithRed:arc4random_uniform(255)/255.0f green:arc4random_uniform(255)/255.0f blue:arc4random_uniform(255)/255.0f alpha:1.0];
       }
      
      
       @end
      

      这就是诀窍。 以下是截图

      在iPhone中

      Screenshot 1 Screenshot2

      iPad中的

      iPad Screenshot1 iPad Screenshot2

      我希望这能解决你的问题。

答案 5 :(得分:0)

请试一试它会对你有帮助。

#pragma mark -- Collection View Delegate and datasource method --
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";


    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, cell.bounds.size.width - 40, 22)];
    title.tag = 200;
    title.backgroundColor = [UIColor purpleColor];
    [cell.contentView addSubview:title];

    return cell;
}

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

    CGFloat width = self.collectionViewSearchCity.frame.size.width/2-20;
    return CGSizeMake(width, 50.0);
}



- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 15, 0,15);
}