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
Related
UICollectionView cell isn't filling the entire screen. When scrolling, it then messes up (leaving spacing on bottom because the image is too short.
Things I've tried:
-Collection View Inset is "None"
-Min spacing is 0 for cells, 0 for lines
-Image inside cell is constraint to 0 on all four sides
-UI collection View is constraint to 0 on all four sides to view controller
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// Even you can set the cell to uicollectionview size
let cvRect = collectionView.frame
return CGSize(width: cvRect.width, height: cvRect.height)
}
Storyboard is made with iPhone 12 Pro max. Simulator: iPhone 12 Pro. Each scroll has a bigger image from the previous.
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 a UICollectionView which is exactly 7 cells wide, with no padding in between. I have done this by adding a flow layout with no interim or line spacing and calculating the width as follows:
public func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
return CGSize(
width: self.frame.width / CGFloat(7),
height: self.frame.height - headerHeight
)
}
This works fantastically within my iPhone app
where rotation isn't enabled. On iPad, when in portrait mode this works fine.
When rotating, firstly the app doesn't redraw, I've attempted to invalidateLayout however this didn't solve the issue. I have to scroll down and I get the following layout
When I tap a cell it does however redraw, but with padding added pushing the 7th cell down onto another row.
I am looking to redraw the cells so all 7 fit on a single row when the app is either rotated or grown and shrunk in split view. How should I handle this correctly?
Add this in your UIViewController class
It will adjust your layout automatically while you change device orientation.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
guard let columnLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
return
}
columnLayout.invalidateLayout()
}
I have a project that has UICollectionView which needs to have paging. I have set horizontal scroll and paging enabled. MinimumInteritemSpacing is 0. I need the cells to be the size of the collection view so I have:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionView.frame.size
}
This sizes the cells properly for both portrait and landscape, but the offset on orientation change is wrong and two cells will be displayed at the same time. What is a good approach to be able to see the same cell on orientation change and for it to be taking up the whole collection view?
I have tried implementing collectionView(_:targetContentOffsetForProposedContentOffset:) This will fix the issue if you stay on one screen, but going to another screen, rotating and going back to the collection view screen will have a bug - there will be again two partially visible cells and the offset will not be the wanted one. This is why I guess there should be another solution to this issue.
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?