         super.traitCollectionDidChange(previousTraitCollection)
 
         if self.collectionView != nil {
-            let indexPath = self.collectionView.indexPathsForVisibleItems.first!
-            self.collectionView.contentOffset = CGPoint(x: CGFloat(indexPath.row) * self.view.bounds.width, y: 0.0)
-        }
-
+            self.collectionView.contentOffset = CGPoint(x: CGFloat(currentRow) * view.bounds.width, y: 0.0)
+        }
+
+    }
+    
+    func updateCurrentRow(to size: CGSize) {
+
+
+        var row = Int(round(collectionView.contentOffset.x / collectionView.bounds.width))
+        if row < 0 {
+            row = 0
+        }
+        currentRow = row
+        
     }
     
     // MARK: - UIContentContainer

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)

        self.tableView.reloadData()
    }
}

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "hcell", for: indexPath) as! ImageCollectionViewCell
        let imageView = cell.imageView
        imageView!.kf.setImage(with: self.imageURLs[indexPath.row])
        return cell
    }
    
