UICollectionViewCell starts after UICollectionViewReusableView width - ios

I'm working on UICollectionView inside UITableViewCell. Using "UICollectionReusableView" as header view. The width of header view is 100. Before I introduce header view the CollectionViewCell started at the beginning of CollectionView, which is as expected. After I put header view, CollectionViewCell starts at end of header view width. I think somewhere position is not set properly.
CollectionView Scroll direction is Horizontal. I have used UICollectionViewFlowLayout to present the CollectionViewCell.
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 5.0, left: 5.0, bottom: 5.0, right: 5.0)
layout.minimumInteritemSpacing = 8.0
layout.minimumLineSpacing = 5.0
layout.scrollDirection = .horizontal
layout.sectionHeadersPinToVisibleBounds = true
collectionView.collectionViewLayout = layout
I have set the size of the header view using "referenceSizeForHeaderInSection" delegate.
Any hints where I might be wrong.

Instead of using UICollectionViewFlowLayout, you should subclass this and override the method
layoutAttributesForSupplementaryViewOfKind:
And give your frame accordingly

Related

UICollectionView content hidden by TabBar

The problem
Hello, community! I created a UICollectionView programmatically and want it to display images that take the full size of the screen, surpassing the top safe area but respecting the bottom safe area, since at the bottom I have TabBar. The issue with the current UI is that I got it to ignore the top safe area, but the tab bar hides part of the content since the collection takes the size of the whole screen (Video). I'm trying so that my UI looks like the TikTok home page where the collectionView ends when the tab bar starts.
My Code
In ViewDidLoad:
let layout = UICollectionViewFlowLayout()
self.edgesForExtendedLayout = UIRectEdge.bottom
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: view.frame.size.width, height: view.frame.size.height)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView?.register(PostCollectionViewCell.self, forCellWithReuseIdentifier: PostCollectionViewCell.identifier)
collectionView?.isPagingEnabled = true
collectionView?.dataSource = self
collectionView?.showsVerticalScrollIndicator = false
collectionView?.showsHorizontalScrollIndicator = false
view.addSubview(collectionView!)
In ViewDidLayoutSubviews:
collectionView?.contentInsetAdjustmentBehavior = .never
collectionView?.frame = view.bounds
What I've tried
layout.itemSize = CGSize(width: view.frame.size.width, height: view.frame.size.height - (self.tabBarController?.tabBar.frame.height)!)
Substracting height of tabBar to CollectionViewFlowLayout
collectionView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: self.tabBarController!.tabBar.frame.height, right: 0)
collectionView?.contentSize = CGSize(width: view.frame.size.width, height: view.frame.size.height - self.tabBarController!.tabBar.frame.height)
Many Thanks!
You should use auto-layout instead of setting the frame of your collection view manually. After view.addSubview(collectionView!) put:
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
This pins the bottom anchor of the collection viewto the bottom safe area of your view, which should account for the tab bar.
You should remove collectionView?.frame = view.bounds from viewDidLayoutSubviews.
Also you should not set your itemSize in viewDidLoad because the view's frame may change. You can set it in viewDidLayoutSubviews.

horizontal and vertical separator line in UICollectionview

I want to make separator lines between collection View cell with a specific color(grey), without changing the BG color of Collection View.
enter image description here
You can use the UIView inside the collectionViewCell and width 1px or 2px
Change the background color of the superview to grey and add edge insects to the collectionView using the below code in viewDidLoad life cycle method:-
private let spacing:CGFloat = 2.0
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: spacing, left: spacing, bottom: spacing, right: spacing)
layout.minimumLineSpacing = spacing
layout.minimumInteritemSpacing = spacing
self.collectionView?.collectionViewLayout = layout

Cell unwanted centered vertically in UICollectionView

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
}()

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