2017-03-02 51 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) 

} 

} 

谢谢:)

相关问题