3

所以它們也同樣吸引我試圖使用collectionViewLayout sizeForItemAtIndexPath同時在collectionView只顯示一個項目和水平滾動,我已經試過這樣:collectionViewLayout sizeForItemAtIndexPath - > CGSize奇怪的行爲

- (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,但我想在整個紫色。

所以請指導我。我很困惑。 :)

+0

可以嘗試打印您的收藏視圖幀sizeForItemAtIndexPath並在viewDidAppear比較? – abarbier

+0

我試過它的兩個框架都是一樣的 –

+0

好吧,你可以打印collectionview的headerview和footerview大小 – abarbier

回答

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 
+0

什麼是「CellforrowAtIndexPath:」? –

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 
    } 
} 
+0

我已經列出了適當的約束條件,並檢查了多次,但沒有得到。也試過這個。 –

1

首先,您不需要以編程方式創建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

+0

仍然不變 –

+0

您面臨的問題是什麼? – PGDev

+0

我發佈的屏幕截圖。我得到了同樣的細胞。視圖在整個單元格中不呈紫色。它有一些空間,你可以在截圖中看到 –

0

你有一些調試選項檢查什麼是對的理由:

  • NSLog()框架您UICollectionView
  • NSLog()CGSize檢查實際大小返回
  • 嘗試設置硬編碼尺寸檢查行爲。
  • 如果使用autoLayout,請在刪除約束後嘗試。

希望這會有所幫助。

+0

嘗試了一切,但仍然無法想出:) –

0

好吧,所以我想通了。 讓我先發布我的故事板佈局。

我花了iPhone6佈局,獲得以下約束集合視圖

設置佈局

  1. 固定集合視圖的寬度使之與視圖的

    Image 1

  2. 下一頁我修正了收藏視圖的縱橫比,以便它始終是方形

Image 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 1Screenshot2

    在iPad上

    iPad Screenshot1iPad Screenshot2

    我希望這能解決您的問題。

    +0

    你是否按照這裏提到的所有步驟。 它在這裏工作得很好。 –

    +0

    另外,如果您已經實現了任何佈局代碼,就像在自定義佈局中一樣,請刪除它們,並讓佈局保持爲故事板的流程(默認)。 –

    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); 
    }