2017-03-02 49 views
1

我使用collectionview以全屏顯示圖像並允許在不同圖像之間滾動。但是當方向改變時它不能正確顯示。 Here image of screen after orientation change。而這是我的代碼如何設置collectionview swift的單元格位置?

override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) { 


// collectionView.reloadData() 
    collectionView.collectionViewLayout.invalidateLayout() 
    if visibleCell != nil { 

    collectionView.selectItem(at: visibleCell as IndexPath?, animated: true, scrollPosition: .centeredHorizontally) 

    } 
} 


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return images.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCollectionViewCell 
     print(cell.frame) 
     cell.SectionImage.image = images[indexPath.row].image 

     return cell 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    return CGSize(width: self.view.frame.width, height: self.view.frame.height) 

} 

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

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

回答

0

使用UICollectionView sizeForItemAtIndexPath方法來定義的高度和電池的寬度。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    let leftAndRightPaddings: CGFloat = 15.0 
    let numberOfItemsPerRow: CGFloat = CGFloat(NUMBER_OF_ITEMS_PER_ROW) 

    let bounds = UIScreen.mainScreen().bounds 
    let width = (bounds.size.width - leftAndRightPaddings * (numberOfItemsPerRow+1))/numberOfItemsPerRow 
    let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout 
    layout.itemSize = CGSizeMake(width, width) 

    return layout.itemSize 
} 

試試吧。你會得到你的預期結果。

+0

No.its不正確的答案。單元格的寬度和高度是正確的,但是當方向改變時,單元格位置也不能保持正確的方式 –

+0

@JayPatel當設備方向改變時重新加載collectionView。 –

0

首先設定用於細胞和線條的最小間距爲0

enter image description here

之後,通過使用委託方法

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

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { 
    dispatch_async(dispatch_get_main_queue()) { 
     self.collectionView.reloadData() 
    } 
    print_debug("change orientation") 
} 

其返回細胞等於UICollectionView的大小在同樣的情況下爲我工作。

+0

沒有爲我工作。 –

0

您可以從UICollectionViewDelegateFlowLayout代理方法sizeForItemAt indexPath(:)中給出UICollectionView中單元格的大小。

// MARK: - Collection view flow layout delegate methods 

extension ViewController: UICollectionViewDelegateFlowLayout { 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    let numberOfCell: CGFloat = 1.0 // Give number of cells you want in your collection view. 
    let screenWidth = UIScreen.main.bounds.size.width 
    let cellWidth = screenWidth/numberOfCell 
    let cellHeight = collectionView.frame.size.height 
    return CGSize(width: cellWidth, height: cellHeight) 

} 

} 

謝謝:)

相關問題