2017-03-02 38 views
1

私はcollectionviewを使ってフルスクリーンで画像を表示し、異なる画像間でスクロールすることができます。方向が変わると正しく表示されません。 Here image of screen after orientation change .ANDこれは私のコード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はデバイスの向きが変わるとコレクションを再ロードします。 –

0

まずデリゲート方法

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

enter image description here

に細胞や線の最小間隔を設定します同じ状況で私のために働く。

+0

私のために働いていません。 –

0

UICollectionViewのセルのサイズは、UICollectionViewDelegateFlowLayoutの代理メソッドsizeForItemAt indexPath(:)から得ることができます。

// 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) 

} 

} 

感謝:)

関連する問題