I used Autoresizing to make proper size of image for all devices . Inside UICollectionView cell put a UIView. Over UIView I placed one ImageView with equal width of view and another view with some content. Now I use Autoresize for all devices But the size of image got differ for iPhone 6s Plus and iPad .I changes with all contents Mode but it remains same. My code for CollectionViewLayout given below.
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == collection || collectionView == collection_favourite{
return CGSize(width: display_width / 2, height: 290)
}
}
As I used Debug view hierarchy it seems that my ImageView resize exactly that I want(shown into below image with blue shade is my imageView) but image shown over it got deflected, or not showing properly .I don't have an idea how to deal with it ,I make so many changes but not resolve .Please help to get this .Thanks in advance
Here are the screen shot of my storyboard
enter image description here
What is the Content mode of the UIImageview? Are you calling layoutIfNeeded in cellForItemAtIndexPath?
Related
So I'm normally use to making square sized collection view cells fit perfectly on all screens. However once I start adding spacing and section insets I run into this problem. I am also using the storyboard.
This is how I would like all of the cells to look on each screen.
iphoneX
I have the minimum space for cells and lines at 10.
And for the right and left section insets they're set to 10 too.
This is the result I get on other devices ( iPhone 7/XsMax)
You have to set cell size programmatically by implementing func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize method which is in UICollectionViewDelegateFlowLayout protocol.
In this method you can set collection view cell's width
(UIScreen.main.bounds.size.width/2)-10
I have a UIViewController that I present from my main screen, which with Xcode 11 new presentation style, is dismissable by dragging down (modal style). Inside of this view controller there is a collection view with a full width and full height cell inside of which there is an imageView that occupies the whole cell.
My collectionView is meant to be paginated, and has a method sizeForItemAt to make the cell the size of the cell:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)
}
What happens is that when I scroll past the collection view's content bounds, the cell get completely distorted and I get the following error:
The behavior of the UICollectionViewFlowLayout is not defined because the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
I have read a few posts about it, and I've tried a few things:
Presenting the UIViewController as full screen (it still distorts the cell when part the horizontal bounds of the collView)
Changing the content inset adjustment behavior of the collection view to ".never"
Removing the bounce
Taking out the image (it does work, as it doesn't distort the cell, but defeats the purpose of design)
Changed constraints of an image from equal to superview to equal height and width to cell and centered in the cell
I'm really racking my brain as to what can be going wrong, to the point that I might be thinking it might be a bug with Xcode 11's presentation style, however, I have no way to be sure of this.
What might be going wrong?
I have collectionview inside tableview cell and I use nib for my collection view cell (in which I use autolayout for my imageview and labels and it is on Freeform mode). I'm setting cell size in tableviewcell class which is handling the delegate for collectionview inside it by this method:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 200, height :150)
}
It always works but after Xcode 11 it doesn't.
I have the same problem. And my solution is to change the Estimate size to None in the Xcode 11.
You set collectionview Estimate size to None in Xcode 11. The reason for this is that cells in a UICollectionView can now self-size with Auto Layout constrained views in the canvas. To opt into the behavior for existing collection views, enable “Automatic” for the collection view’s estimated size, and “Automatic” for cell’s size from the Size inspector.
If deploying before iOS 13, you can activate self sizing collection view cells by calling performBatchUpdates(_:completion:) during viewDidLoad()
Ref:
https://developer.apple.com/documentation/xcode_release_notes/xcode_11_release_notes
As Anh Tuan said in another answer here, you just need to change the Estimate Size to none in the Size Inspector of the Collection View from the Storyboard.
But if you wanna do this programmatically, you can try this code:
let layout = myCollectionViewReferenceHere.collectionViewLayout as! UICollectionViewFlowLayout
layout.estimatedItemSize = .zero
This problem is coming in Xcode 11. Go to the attribute inspector and change the estimateSize to None. will fix every thing.
I have created a collectionview with a tableview embeded into the collectionview cell. The idea is that the collectionview makes it able to scroll horizontal, while the tableview makes it able to scroll vertical for each section. Everything is set up in storyboard using autolayout, and works as it should, the only issue is that when i rotate the screen from portrait to landscape, the top if the collectionview or tableview is clipped.
How it currently looks in Portait mode
How it currently looks in Landscape mode (This is where the problem is)
I have attached two images: 1 from portait mode, which is how it should look like, and a second image of landscape mode, where the first item in the tableview and the tableview header is clipped. I suspect something in the view is not updated correct, but i am not sure what?
Thanks in advance
please set collection view cell width equal of collection view
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width: CGFloat = collectionView.frame.size.width
let height: CGFloat = SET_HEIGHT_THAT_YOU_WANT //collectionView.frame.size.height
return CGSize(width: width, height: height)
}
don't forget to set UICollectionViewDelegateFlowLayout
I am working on an iOS app in swift 4. I have implemented collection view and there are two cells at the front as shown in the image:
When I run my app on bigger screen sizes like iPhone 6/7/8 Plus, then it Shows two complete cells with one-half cell. What I want to achieve is that it shows two cells in front of every screen size, Please help.
Implement the UICollectionViewDelegateFlowLayout protocol in your ViewController, then implement:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellSize = //calculate cell size based on the width of the view and padding between cells
return cellSize
}
Check out the documentation for the UICollectionViewDelegateFlowLayout and implement its methods to modify the spacing between the cells.