How to scale and show next/previous items in a Collection View? - swift

I'm working on a collection view that should look like this:
The scroll view works but I want to make my collection view peak the next item like in the screenshot, and when scrolling it scales the normal size of course.
I couldn't manage to do that with only enabling paging (of course).
Thank you all for any help!
Edit:
This kinda helps what I want to achieve, but it still lacks scaling and does not work near good while scrolling.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return -100
}

You can of course do this with collectionView or scrollview, however I recommend using some well made external library for this. Try looking at this Library You can also Install it with CocoaPods , looke here: cocoapods link
I think it will serve you well.
If you want to do this entirely by yourself try taking advantage of this UICollectionViewDelegate function with implementing CGAffineTransform scaling :
func collectionView(_ collectionView: UICollectionView, willDisplay willDisplayCell: UICollectionViewCell, forItemAt forItemAtIndexPath: IndexPath) {
guard let cell = willDisplayCell as? YourCell else { return }
cell.transform = CGAffineTransform(scaleX: 2, y: 2)
}

If you want to do all in one at go, please go Ui collection view delegate.function with implementing CGAFFineTransform scaling.

You can use ICarousel pod for doing this. You can also set another Carousel types.

Related

UICollectionView Layout Buggy

Hey guys I’d love some help on an issue I am facing with uicollection view. It’s a uicollectionview inside a .xib with custom cells that I’m loading through UINibs. I almost get the result I’m looking for but there’s some awkward spacing. Also as a note I’m using the layout delegate to calculate the size of the text and then setting the cell but it’s still awkward every time I generate it. Thanks guys.
Example 1
Example 2
Here is the code I am using:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// dataArary is the managing array for your UICollectionView.
let item = self.visibleSuggestion[indexPath.item]
var itemSize = item.size(withAttributes: [
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 13)
])
itemSize.width += 20
itemSize.height += 15
return itemSize
}

Horizontal collection view with different height of cells

I am trying to do some horizontal collection view with dynamic height of each cell. Like citymapper does when you have aready choosen a trip. ¿Has anyone got any idea of how to do this? Even though if it's not with a collection view.
Video of the behaviour
One way of doing this is implementing the UICollectionViewDelegateFlowLayout of the collectionView.
One of the methods of this delegate is final public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize. Here you can return the size of the collectionCell depending on the content or whatever you use to determinate the height.
Hope it helps.

Header view of UICollectionView keeps duplicating on scroll - How to create fixed (non-sticky), single collection view header?

I wanted to implement a view hierarchy like the following so that the ENTIRE view would be scrollable:
UIScrollView
Image View
Collection view
But a lot of people on here have said that it is better to use the header that comes along with the collection view. I've done that but now I have a new problem: as I scroll the collection view, any configurations I've done to the header cell in the viewForSupplementaryElementOfKind function is duplicating (Eg: If I programmatically create a new view in viewForSupplementaryElementOfKind function, this view will keep creating as I scroll)
I kind of get that this is happening because I'm dequeuing the header using dequeueReusableSupplementaryView. But I've tried searching on Apple docs and there are no other codes I can use to instantiate the header view without making it reusable.
Is there any way I can create a view controller as described above without using UICollectionView?
I've tried setting the number of sections to 1 hoping that it would only be reused ONCE but it doesn't work.
Edit: Also tried setting header size using UICollectionViewDelegateFlowLayouta and using UICollectionView instead of UIViewController and UICollectionViewDataSource etc.
func collectionView(_ collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
{
switch kind {
case UICollectionView.elementKindSectionHeader:
guard
let headerView = collectionView.dequeueReusableSupplementaryView(
ofKind: kind,
withReuseIdentifier: "DiscoverHeader",
for: indexPath) as? DiscoverHeader
else {
fatalError("Invalid view type")
}
// Rotating arrow image
headerView.arrowImg.transform = headerView.arrowImg.transform.rotated(by: CGFloat.pi/0.67)
return headerView
default:
assert(false, "Invalid element type")
}
}
Why not UIImageView and UICollectionView inside UIScrollView?
You can definitely create a UIScrollView and add an UIImageView and a UICollectionView in it.
But that won't work as expected. This is because you're embedding a scrollView(UICollectionView) inside another scrollView. Scrolling the collectionView vertically will hamper the scrolling of the outer scrollView.
Solution:
Try giving the height of the header view using UICollectionViewDelegateFlowLayout's method,
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 100.0) //give the height as per your requirement...
}
Also, set the contentMode of imageView as Aspect Fit / Aspect Fill.

Fitting all items into one collection view row

Is there a way to make all items in a collection view fit in one row and not automatically place items in an another row? I'm trying to copy something like Instagram's story display like the link below:
However, In my collection view, I'm getting a count from a server, and displaying the information in a collection view but it always goes to the next line after 3 items are displayed.
// number of sections function
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pictures.count
} // end of function
I think you don't want Vertical scrolling in Collection view.
For horizontal scrolling in collection view, select collection view in storyboard and change scroll direction to horizontal.
Refer this below pic:
you can add collectionViewLayout delegate method like this. CGFloat(3.0) is how many item you want, you have to divide it from collectionView's width
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemWidth: CGFloat = ((collectionView.bounds.size.width) / CGFloat(3.0))
return CGSize(width: itemWidth, height: collectionView.bounds.size.height)
}

Easy UICollectionView Custom Layout

I want to create a custom layout which should look like the picture here: Custom Layout
I found a tutorial by Ray Wenderlich (https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest) which does more or less the same but it was to complicated for me so I wasn't able to shift this to my needs.
I just need the easy layout, and I also know the size for my cells, I don't have to calculate them.
Could anyone please help me.
In
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
of UICollectionViewDataSource create a switch for the variable indexPath.row and set the size of the cell:
cell.frame = CGMake(height: myHeight; width: myWidth)