Cell unwanted centered vertically in UICollectionView - ios

I have a UICollectionView which is supposed to display one cell at a time. My problem is, that the cell is centered vertically which is not a desired effect and I can't seem to find where this is caused.
This screenshot shows that the cell has a padding of 46 to the top (as well as to the bottom). The cell is 308 high. Adding the 2x46 that comes to 400px which is the height of the collectionView.
I have tried to add UIEdgeInsets to the collectionView and set them all to 0 but that didn't do it.
Any idea why my collectionView would do this? Thanks!!
This is how I declare my collectionView:
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
cv.register(AddCardCell.self, forCellWithReuseIdentifier: "CardCell")
cv.backgroundColor = .white
cv.showsHorizontalScrollIndicator = false
cv.isPagingEnabled = true
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}()

Related

CollectionView does not scroll when added to view

Here is my code:
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = .greatestFiniteMagnitude
layout.minimumLineSpacing = 15
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: screenWidth*0.2, height: screenHeight*0.15)
collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: screenWidth*0.9, height: screenHeight*0.15), collectionViewLayout: layout)
collectionView?.layer.zPosition = 10
collectionView?.register(PlantSnapshotCell.self,forCellWithReuseIdentifier:PlantSnapshotCell.identifier)
collectionView?.backgroundColor = .clear
collectionView?.alwaysBounceHorizontal = true
collectionView?.bounces = true
collectionView?.showsHorizontalScrollIndicator = false
collectionView?.dataSource = self
collectionView?.delegate = self
scrollView.addSubview(collectionView!)
view.addSubview(scrollView)
It renders in the view, but any sort of interaction does not work. The width of the collectionView is 90% the width of the superview. Scrolling horizontally does not work.
Why you are adding collectionView as a subview to the scrollView?

UICollectionView full width and height problem

UICollectionView width is not full width, One cell per row
UICollectionView
private func setCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.showsHorizontalScrollIndicator = false
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.isPagingEnabled = true
collectionView.register(ImageCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
}
UICollectionView -> Constraint
NSLayoutConstraint.activate([
view.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor),
view.trailingAnchor.constraint(equalTo: collectionView.trailingAnchor),
view.topAnchor.constraint(equalTo: collectionView.topAnchor),
view.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor)
])
UIColletionViewCellSize
By adding UICollectionViewFlowLayoutDelegate
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}
ImageCollectionViewCell
class ImageCollectionViewCell: UICollectionViewCell {
....
private let imageView: UIImageView = {
let img = UIImageView()
img.contentMode = .scaleAspectFill
img.translatesAutoresizingMaskIntoConstraints = false
return img
}()
....
}
ImageCollectionViewCell -> imageView Constraint
addSubview(imageView)
NSLayoutConstraint.activate([
leadingAnchor.constraint(equalTo: imageView.leadingAnchor),
trailingAnchor.constraint(equalTo: imageView.trailingAnchor),
topAnchor.constraint(equalTo: imageView.topAnchor),
bottomAnchor.constraint(equalTo: imageView.bottomAnchor)
])
UICollectionView FlowLayout Warning
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.
The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {0, -44}; contentSize: {0, 0}; adjustedContentInset: {44, 0, 34, 0}> collection view layout: .
This is Keep Happening
https://gph.is/g/EqNL8jr
UICollectionView Warning Fix
if #available(iOS 11.0, *) { collectionView.contentInsetAdjustmentBehavior = .never }
else { automaticallyAdjustsScrollViewInsets = false }
Scale to fill was giving bigger image than UIImageView size
img.clipsToBounds = true

Add horizontally scrollable filter options under UISearchController

I am developing a page that allows the user to search contents in a table, and I want to add some filter options under the search bar. I've added the UISearchController programmatically into the navbar, but I want to know how to add a list of horizontally scrollable filter options under the search bar (Just like the layout on the right in this picture: https://i.stack.imgur.com/XrjIq.png). Thanks!
I realize that this is probably something that has been asked/answered before, but I cannot find a single page discussing this. If you have a link to a solution I would really appreciate it!
You can create a horizontally scrolling collectionView with filter buttons in them and set it below the UISearchController.
You can create a collection view like this programmatically:
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.minimumInteritemSpacing = 0
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
cv.dataSource = self
cv.delegate = self
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}()
Now you can add items to this collectionView using cellForItemAtIndexPath, numberOfItemsInSection and other delegates.
You would need to then set constraints on the collectionView like this:
collectionView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
collectionView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
collectionView.topAnchor.constraint(equalTo: searchController.bottomAnchor).isActive = true
collectionView.heightAnchor.constraint(equalToConstant: 44).isActive = true
Let me know if you need further explanation.

hidesbarsonswipe mutates view size

I am having trouble with this piece of code:
navigationController?.hidesBarsOnSwipe = true
My navigation controller's root view controller is a UICollectionViewController. The blue view below is a cell that represents the user's current screen. I think the problem is that I need to resize the cell when the navigation bar hides.
I set the cell size like this:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height - 44)
}
I am not breaking any constraints, and I am very sure everything is set up properly. But when I swipe up, this happens:
Before
After
As you can see, the view gets shortened. I can't find those measurements anywhere in my code.
Is there a way to ensure that the view gets resized properly?
Put this in your viewDidLoad() and you should be good to go.
self.edgesForExtendedLayout = []
yourBlueView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(yourBlueView)
yourBlueView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
yourBlueView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
yourBlueView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
yourBlueView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
Edit:
Change yourBlueView to the name you've given that particular UIView. Let me know if you get stuck, but do try to figure it out first. That's the best way to learn it and retain it.
The bar on the bottom takes at least 44 and I notice it's going under the nav bar at the top. Remove your hard code sizing which is too high, and only use the auto layout
Second Edit: (This is a non-autolayout approach)
let reuseIdentifier = "Cell"
This code goes in viewDidLoad:
// Do any additional setup after loading the view, typically from a nib.
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
let displaySize = UIScreen.main.bounds
let displayHeight = displaySize.height - 40
let displayWidth = displaySize.width
layout.itemSize = CGSize(width: displayWidth, height: displayHeight)
if let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout) {
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView.backgroundColor = UIColor.white
self.view.addSubview(collectionView!)
}

How to make UICollectionViewFlowLayout stay inside a UICollectionView

I have built a custom calendar using collection view. My problem is that when I add UICollectionViewFlowLayout it covers the whole screen and doesn't stay inside UICollectionView. How do I make the UICollectionViewFlowLayout stay inside UICollectionView? Here is my code:
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 100, left: 10, bottom: 1, right: 10)
let width = UIScreen.mainScreen().bounds.width
layout.itemSize = CGSize(width: width/10, height: 35)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
collectionView!.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView!)
Where I want the calendar to be:
This is how it covers the whole screen:
This line:
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
is creating a new instance of UICollectionView, and setting its frame to be the same as self.view (hence it covers the full screen). I suspect you actually want to use an existing instance of collection view which is established in a storyboard (and is probably being presented - but behind the new one!). Check in your storyboard to see whether the CollectionView is hooked up to the collectionView property of your view controller:
If so, you can just comment out the above line (and possibly the next two lines as well), since the links will be established when your view controller is instantiated.
If you do not have a storyboard instance, then just amend the frame to suit the position and size that you want, rather than using self.view.frame.

Resources