0

just as to make it clear all other similar questions are asking for objective C or either not in storyboard hence my question is valid..

So I want to do is create a full screen image slider. Im using collection view to do it. but the outcome is not as expected especially the scrolling and the way the cell is displayed.

here is my code for collection view:

import UIKit
import Firebase
import FirebaseDatabase
import SDWebImage

 class DoceFullImageVIew: UIViewController {


@IBOutlet weak var colltionView: UICollectionView!

var images = [ChudoInsta]()

//  var imgArr = [ChudoInsta]()

var dbRef: DatabaseReference!



let viewImageSegueIdentifier = "doceImageSegueIdentifier"

override func viewDidLoad() {
    super.viewDidLoad()
    
    dbRef = Database.database().reference().child("wedding/doce_pics")
    
    loadDB()

   }

  func loadDB(){
    dbRef.observe(DataEventType.value, with: { (snapshot) in
        var newImages = [ChudoInsta]()
        
        for chudoInstaSnapshot in snapshot.children {
            let chudoInstaObject = ChudoInsta(snapshot: chudoInstaSnapshot as! DataSnapshot)
            newImages.append(chudoInstaObject)
        }
        
        self.images = newImages
        self.colltionView.reloadData()
    })
  }



 func printvalue() {
    
    print("This is the doce full image firebase \(images)")
    // print("This is the doce full image firebase \(images)")
  }

}


extension DoceFullImageVIew: UICollectionViewDelegate, UICollectionViewDataSource {

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

   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? DataCollectionViewCell
    
     let image = images[indexPath.row]
    
     cell?.img.sd_setImage(with: URL(string: image.imageUrl), placeholderImage: UIImage(named: "image1"))
    
     return cell!
    
     }

   }

   extension DoceFullImageVIew: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      let size = UIScreen.main.bounds
      return CGSize(width: size.width, height: size.height)
    
    
   }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    //return UIEdgeInsets(0, 5, 0, 5);
    }

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

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

Can some one point out what is wrong and why the output comes like this images chopped : https://drive.google.com/file/d/1JzbX-B0dhuInJ_ydwFWyuYTNq6d_lZS_/view?usp=sharing

https://drive.google.com/file/d/1KdPKkcvdAR4lMEY-0nwthsFlS2OxC_Te/view?usp=sharing

and in landscape the images are chopped as well: https://drive.google.com/file/d/1RgqUCtUL_L2Xystg1eDDm7ZMBBhqaZvj/view?usp=sharing

here are some of my story board screenshots: https://drive.google.com/file/d/1NMzvSHfEHWil_HsMtELmU4kd2Awe4nXt/view?usp=sharing https://drive.google.com/file/d/1Q6DiBWWXGCzZ6bVKn9dkprMeluXuuonW/view?usp=sharing https://drive.google.com/file/d/1-CB_XtGSI_rXpqbckytGgNrdaH1upaUy/view?usp=sharing https://drive.google.com/file/d/1r4gAV2p_VS4Ix5ht_HPQVe7Rs8LeKtpX/view?usp=sharing

my flow delegate: https://drive.google.com/file/d/1BjIBwvTVe0Ew_MAKBpeorkowQlDdD3eT/view?usp=sharing

Im trying to achieve a fullscreen image slider with collection view(images are retrieved from firebase)

Vlad V
  • 1
  • In storyboard, select the collection view, go to size inspector, change `Estimate Size` to `None` and set all values for min spacing to 0. I'm not sure this would help, but I've had similar issues when estimated size is set to `automatic` in storyboard. – Jithin Aug 23 '20 at 12:46
  • and use `collectionView.bounds` instead of `UIScreen.main.bounds` – Jithin Aug 23 '20 at 12:53
  • Try invalidating the layout. check this one: https://stackoverflow.com/a/54653303/5443937 – Jithin Aug 23 '20 at 16:18
  • @Jithin Thanks a lot it worked.. but now the problem is that when I rotate the emulator device than the same problem reappears. Also it jumps to the last cell(image) or some other cell of the collectionView. here how it looks: https://drive.google.com/file/d/19vITS7ZHHlMiGfzjzrL--q4CT_YqwHmO/view?usp=sharing. here I have used multiple devices to replicate the problem which occurs when I rotate the phone from portrait -> landscape -> portrait. – Vlad V Aug 23 '20 at 17:02
  • @Jithin where in the code should I use the invalidate layout method? – Vlad V Aug 23 '20 at 17:29
  • Its all in the above link. You will get callback inside `viewWillTransition(to size: CGSize, with coordinator` method when device is rotated. invalidate the layout inside that method. – Jithin Aug 23 '20 at 17:40
  • @Jithin I think you got me wrong. The cells are sizing properly when orientation is changed only now the problem is that the cells(image) changes into some other random image when I rotate the device. And comes back to original image when I rotate it back. see this image: https://drive.google.com/file/d/19vITS7ZHHlMiGfzjzrL--q4CT_YqwHmO/view. refer to my above comment. btw the problem still persists after adding viewWillTransition(to size: CGSize, with coordinator – Vlad V Aug 23 '20 at 18:14

0 Answers0