Map view getting shrunk in Dynamic table view cell - swift

In a swift iOS app, I have a tableView in viewController with 2 prototype cells. One cell has two text boxes (Cell) and other cell has one UIview (mapView in MapCell) in which I have to display a google map view with some location markers. I have set leading,trailling , top and bottom constraints for all the things in the cell. Also, included following for dynamic cells.
chatMessagesTableView.rowHeight = UITableViewAutomaticDimension
chatMessagesTableView.estimatedRowHeight = 100
First cell gets dynamic height depending upon content size, however MapCell is shrunken, tried a lot of things and nothing is helping, please suggest what can I do more. Here is a related code and tableview cell image.
let cell = tableView.dequeueReusableCell(withIdentifier: "MapCell", for: indexPath) as! tableViewCell
googleMapView.frame = CGRect(x: 0, y: 0, width: 400, height: 300)
let camera = GMSCameraPosition.camera(withLatitude:self.tripLocation.latitude, longitude: self.tripLocation.longitude, zoom: 15)
googleMapView.animate(to: camera)
cell.mapView.addSubview(googleMapView)
cell.mapView.sizeToFit()
cell.updateConstraints()
cell.updateConstraintsIfNeeded()
One more problem is when I scroll down the table, the next cells gets messed up with the constraints , for ex, text alignment! Please help me solve this. I am reading lot of tutorials on constraints and layout, however nothing is really working out!

Related

IndexPath.row of CollectionView gets changed while scrolling

I am new in swift, the issue I am facing is I have a collection view of height 2000. when I scroll down to the bottom, the indexPath.row gets changed due to which the logic gets disturbed. I understood that this happens due to the reusability of cells but how should I stop this. This is how I am initializing cellForItemAt.
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "analysis", for: indexPath) as? analysisCollectionViewCell
if goalData.notesArr.count == 0 {
cell?.viewNotes.isHidden = true
cell?.constraintHeightNotesView.constant = 0
self.constraintHeightMainView.constant = self.constraintHeightMainView.constant - 400
}else{
cell?.viewNotes.isHidden = false
cell?.constraintHeightNotesView.constant = 470
}
I am changing the height of a few components(cell?.constraintHeightNotesView.constant) as well as of my mainView also(self.constraintHeightMainView.constant).
Please, edit the question so that We can see your code in full
This cell initializing method is correct
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "analysis", for: indexPath) as? analysisCollectionViewCell
But I can't understand why you need an collection view height 2000?
If you want to setup layout the collection view in full screen, You need to set constraints of collection view to the edges of the main view of your ViewController
if you collection view have a many elements, you will be able to scroll it.
This functionality is available without additional configuration.

how can i set a dynamic height to collection view cell in swift?

HI everyone i'm looking for a working method that allows to dynamically set the height in my collectionViewCell because I tried various other answers without success.
My situation is pretty simple, I have a cell with an image, a UIlabel that displays username and a UILabel which contains some text. It's a common Comment cell for a post. My Screen
I tried this method but it doesn't work precisely because the height is too much compared to what is necessary
private func estimatedFrameForText(text: String) -> CGRect {
let size = CGSize(width: 270, height: 80)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
return NSString(string: text).boundingRect(with: size, options: options, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17)], context: nil)
}
so my question is: there is a correct way to implement dynamic size on collection view cell like layout attribute wrap_content for Android?
UPDATE
I solved my problem using constraints and Automatic Estimated Size on Collection View. Setting up all constraints I forced the width of LABEl to be equal to the cell width and for the height I set the top and bottom spacing for the UILabel (with lines 0) to be equal to its content view (the one standard for the UiCollectionViewCell)
IT'S also important to set the size of the collection view cell to automatic
VIEW IMAGE

Paging with left inset on small cells in UICollectionView

I have a UICollectionView filled with dates ranging from one date to another. I have implemented the UICollectionView with IGListKit. I would Like to enable paging on each cell so that each cell will land in the middle of the circle when it scrolls past it. (Like below) I have tried a few ways of paging, but these don't centre directly in the middle of the circle (As seen in the second image). What is happening is, the cells are getting paged to the left-hand side, I would like there to be an inset of 20 so that it fits into the circle
To enable the current date to show on the left-hand side in the circle when the view first loads, I have the semantic set to Force Right-to-Left and I also have edgeInsets which are:
left: screenWidth-62, right: 20)
The width of the cell is 42 and the inset at the side is 20, therefore the first cell will sit in the circle with these insets. So when the view first loads, the collection view looks like this:
I basically want each cell to land inside of this circle when it scrolls past (if the scrolling stops)
This is what I have so far:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView == self.collectionView {
var currentCellOffset = self.collectionView.contentOffset
currentCellOffset.x += (self.collectionView.frame.width / 2) + 20
if let indexPath = self.collectionView.indexPathForItem(at: currentCellOffset) {
self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
}
}
and this:
collectionView.decelerationRate = UIScrollView.DecelerationRate.fast
Does anyone know how to solve this?
Thank you
i created a custom collection view layout for paging by cell instead of screen
i also outline a few different ways to customize it with examples in the README
check it out here
let me know if you have any questions about it or need more help

CollecitonViewCell Dynamic With based on Autolayout label Text

I Have a Horizontal Collection View with Cells containing a label that has auto layout that expands on the size of the cell view.
Im using Xcode 8 and Swift 3.
How can I make so that my cell size is dynamic based on the text? I mean, I want it to expand, so that Otro Text... Reads complete instead of how its shown right now with the dots.
As You can see on the screenshot, now my large text gets trimmed (The Collection view, is on the area that has the Texts: Todo, Otro Text... Test1).
Hope someone can help or orient me on finding a solution.
You need to make sure that the you add leading and trailing constraint to the label and do not add any width constraint.
Next step is to give an estimatedSize in you collectionViewLayout.
Example
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = CGSize(
width: 100, height: collectionView.bounds.size.height)
}
You need to set the estimatedItemSize in your UICollectionViewLayout to be able to enable self-sizing cells. It is not possible from Storyboards, only available through code.

Swift - Using autoLayout on custom Cell

I'm trying to center horitzonally a button into my custom cell but I can't.
The constraits are OK but doesn't work...
Some screenshots:
What i'm doing wrong?
Thanks!
EDIT1
I'm cheking the TableView, View and the Cell with:
println("TableView width: \(self.tableView.frame.width)")
println("View width \(self.view.frame.width)")
println("Cell width: \(cell.frame.width)")
TableView width: 600.0
View width 375.0
Cell width: 600.0
Cell width: 600.0
TableView and Cell are longer than the view. I think that this is the problem. Why isn't my cell fitting to the width screen?
Maybe the tableView is wider than the window. Try adding top, bottom, left and right constraints to the tableView.
If this is not the case, please give some more information about the constraints you added and the way you added the number label at the left of the cells.