Image does not fit properly in collectionCell

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Image does not fit properly in collectionCell



In Swift 3. My app has a table view and a detail view with is a view controller. Here I added a collection view. An array of images must be presented. The images adapt themselves beautifully if in portrait I check the content mode in scaler to fill. In ladscape it works in aspectFill. Vice-versa, the layout does not fit. I did the following
in cellForItem At:


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = myCollection.dequeueReusableCell(withReuseIdentifier: "cellCollection", for: indexPath) as! CollectionViewCell
cell.cellImage.image = UIImage(named: array[indexPath.item])

if UIDevice.current.orientation.isLandscape{
cell.cellImage.contentMode = .scaleAspectFill
}
else if UIDevice.current.orientation.isPortrait{
cell.cellImage.contentMode = .scaleToFill
}

return cell
}



It does not work. May be I have some inconsistencies from portrait to landscape.
I would appreciate your help. If you want to take a look at the project in git:
[https://github.com/ricardovaldes/tareaFinalHoteles.git1. Thanks in advance.





Your issue is unclear. And why would you want a different content mode depending on the device's orientation?
– rmaddy
22 hours ago





Just because the image fits properly and the scroll of the collection view behaves properly as well. I do not know why at the moment, but very well in scale to fill when in portrait and aspect fill when in landscape. Just take a look at the project in GitHub. thanks.
– acidosulfurico50
22 hours ago





You need to explain what issue you are having. It's not clear. No one should have to build and run your project just to know what problem you are having. That's not how this site works.
– rmaddy
22 hours ago





OK maddy. I want to make my images to display in the scroll of my collection. In portrait it looks very well when content mode is in scale to fill. I use size for item at to try to configure my cell.
– acidosulfurico50
22 hours ago





func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let size = CGSize(width: myCollection.frame.size.width, height: 280) return size }
– acidosulfurico50
22 hours ago




1 Answer
1



Eureka. Finally I did solve this problem. With sizeForItem at, the app could function without problems. I was lost or mislead looking for a way to change the image content mode, but this is not the right way. The correct way is resize once you rotate: for this you need the func willTransition toNewCollection: UITraitCollection. Inside you create a variable layout that is of type UICollectionViewFlowLayout. With the help of a conditional, is possible to define how you want the with and heigt after rotation.This post is clear and concise for those curious: UICollectionView Cell Resizing on Device Rotation.
Here the implementation I did in my project: `


override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape,
let layout = myCollection.collectionViewLayout as? UICollectionViewFlowLayout{
let with = myCollection.frame.size.width
layout.itemSize = CGSize(width: with, height: 280)
layout.invalidateLayout()
}

else if UIDevice.current.orientation.isLandscape,
let layout = myCollection.collectionViewLayout as? UICollectionViewFlowLayout{
let with = myCollection.frame.size.width
layout.itemSize = CGSize(width: with/2, height: 280)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

iPaM rU9R5u w WHIk88rDRLRZY1Zaa1Stx1,jsi TC,ivFzd1E4 9Y gtwpqWF54i MtF2kqRjm
rjDtEtyJpkd146SqtMd,MmlcY8 n7RD,4,iRyHxQxhpmUXcmB b 6L0o13I,c

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results