How to reduce space between 2 message cell in MessageKit Library Swift? - ios

I'm using MessageKit Chat Library. I removed the Avatar View from message cell. But the space between 2 message cells is too much. I want to reduce space between 2 message cell. how can I do that ?
Edit:

I solved problem with cell spacing on cell in CollectionView
if let layout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout {
layout.textMessageSizeCalculator.outgoingAvatarSize = .zero
layout.textMessageSizeCalculator.incomingAvatarSize = .zero
layout.sectionInset = UIEdgeInsets(top: -10, left: 0, bottom: -5, right: 0)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
}

Related

UICollection spacing not working I Don't wont any spacing in uicollectionview

I have tried following but not working
self.hCltVw.constant = (rowHeight * 8.0)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.itemSize = CGSize(width: size, height:rowHeight)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView.reloadData()
collectionView.collectionViewLayout = layout
But still spaing in collectionview row. Dont know why spacing is not working also if i add + 80 in collectionview height spacing is automatically removed.
My Xib Setup
NOTE: the strange thing is if I increase collection-view height
constant then space is decreasing.
i had same problem, i solved it by this may be it will helpful for you .
in storybord select your collectionview and change some settings in attribute inspector.
set estimate size = none
in section insets top,bottom,left,right set this all value 0
set min Spacing (for cell , for line both) = 0
[edited]
try this:-
let width = (view.frame.size.width) / 3
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSize(width: width, height: width)

Uicollection view dynamic height but fixed width

I am working with UICollectionView I have 4 different kind of cells. Every cell is designed in xib file . when i load them in collection view it goes out of screen. i don't know why it is happening. Some cells have fixed height while some have dynamic height (depends on data coming from API). So is there any possible way to solve this issue.
i have tried Estimate size automatic to dynamic
override func awakeFromNib() {
super.awakeFromNib()
self.contentView.translatesAutoresizingMaskIntoConstraints = false
let screenwidth = UIScreen.main.bounds.size.width
widthAnchor.constraint(equalToConstant: screenwidth-20)
}
func setSize () {
let layout = mainCollection.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)}
}
try this!
let screenWidth = self.CollecionView.frame.width

Place the collection view cells vertically instead of horizontally?

How to design UICollectionView in such a way that it fills the collection view cells vertically (column-wise) instead of filling horizontally (row-wise)?
The collection view should be vertically scrollable, I should not change the size of the collection cell and determine the number of rows dynamically?
(YOUR_COLLECTIONVIEW_NAME.collectionViewLayout as? UICollectionViewFlowLayout).scrollDirection = .horizontal
I use this code for it:
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
myCollectionView.collectionViewLayout = layout
For limited number of rows you can set collectionView height and set height of cell and spacings. For example, if you need 2 cells in column, you will write:
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: self.collectionView.frame.size.width / 5, height: self.collectionView.frame.size.height / 2.5)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
you can change this parameters like you need

UICollectionviewCell always placing at the edge of the view

my UICollectionViewCells are always placing at the edges of the UICollectionView. There is a middle space always like this.
What is the reason for that? And I want to keep 3 items per row always. But in this way it just set 2. How to fix this issue?
Please help me.
Thanks
UPDATE
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical //.horizontal
layout.itemSize = cellSize
layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
layout.minimumLineSpacing = 1.0
layout.minimumInteritemSpacing = 1.0
layout.minimumLineSpacing=1.0
btncollectionView.setCollectionViewLayout(layout, animated: true)
btncollectionView.reloadData()
Works for both horizontal and vertical scroll directions, and variable spacing
Declare number of cells you want per row
let numberOfCellsPerRow: CGFloat = 3
Configure flowLayout to render specified numberOfCellsPerRow
if let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
let horizontalSpacing = flowLayout.scrollDirection == .vertical ? flowLayout.minimumInteritemSpacing : flowLayout.minimumLineSpacing
let cellWidth = (view.frame.width - max(0, numberOfCellsPerRow - 1)*horizontalSpacing)/numberOfCellsPerRow
flowLayout.itemSize = CGSize(width: cellWidth, height: cellWidth)
}

UICollectionView crashes when column count changes

I have a UICollectionview and the number of columns are changing randomly. Every time the column count changes I invoke the following function.
func setTheCollectionViewSize(){
if self.activeLockerList.count > 0 {
self.btnLockerDropdown.setTitle("Lockers Set \(self.activeLockerList[0].lockerId)", for: UIControlState.normal)
}
screenSize = UIScreen.main.bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
//if the collection view layout is not invalidated then the app will crash
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
//setting the collectionview size so there will be no white lines in the drawn boxes
var appropriateScreenWidth = Int(self.screenWidth)
while appropriateScreenWidth % Int(self.numberOfLockersPerColumn) != 0 {
appropriateScreenWidth = appropriateScreenWidth - 1
}
let itemWidth : CGFloat = CGFloat(appropriateScreenWidth) / self.numberOfLockersPerColumn
collectionViewWidthLocal.constant = CGFloat(appropriateScreenWidth)
layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
print(self.numberOfLockersPerColumn)
print(screenWidth)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.sectionInset = UIEdgeInsets(top: 10.0, left: 0, bottom: 10.0, right: 0)
self.collectionView.collectionViewLayout.invalidateLayout()
self.collectionView.setCollectionViewLayout(layout, animated: false)
}
When the cell count for each row increases it works fine. But when the count decreases app crashes at the line self.collectionView.setCollectionViewLayout(layout, animated: false)
As you can see invalidating the collectionview with self.collectionView.collectionViewLayout.invalidateLayout() also didn't help. I've been spending hours and hours still without luck. Thanks in advance.
Did you invoke setTheCollectionViewSize() function before collectionView.reloadData() or after?
It should go in the following order. Try and let me know.
self.collectionView.reloadData()
self.setTheCollectionViewSize()
in other words following should be the order when invalidating collectionview
self.collectionView.reloadData()
self.collectionView.collectionViewLayout.invalidateLayout()
Hope it helps you out.

Resources