Swift - Using autoLayout on custom Cell - swift

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.

Related

Shrink UICollectionView reusable header height when hiding subviews or changing subview heights

I have a UICollectionView with a reusable header on top which contains two views. One is another collectionView and under it a filtersView.
This is how I display the header and control its layout dynamically.
let layout = collectionView.collectionViewLayout
if let flowLayout = layout as? UICollectionViewFlowLayout {
flowLayout.headerReferenceSize = CGSize(width: self.collectionView.frame.size.width, height: 70)
}
Now when I hide or change filtersView height constant the collectionView is filling up the whole headerView.
What I need is to shrink the headerView itself.
I have top, bottom, leading and trailing and constant height constraints for collectionView in the headerView and also the same for the filtersView that is underneath the collectionView
Tried playing with priorities and heights programmatically a lot but never succeeded to shrink the headerView to be same height of collectionView when filtersView is hidden or its height is equal to 0.
For demonstration purposes I added a cyan background color to the collectionView and a dark gray color for the whole headerView background.
1st Image filterView height = 50
2nd Image filterView height = 0

Controls at the bottom of UIScrollView not working

The UIButton in the scrollView is visible, but not accessible. I am using Constraints.
My UI structure is this:
- UIView
- scrollView: UIScrollView
- contentView: UIView
- UIButton
- UIButton
- UIButton
- ....
- UIButton
I've already tried to set the contentSize of the scrollView. And the height of the contentView. Next to that I've tried to uncheck the checkbox Adjust Scroll View Insets in the storyboard of that ViewController without any luck. I've also set the priority of the Align Center Y to 250, and the priority bottom space to 250 of the contentView.
func updateScrollViewSize() {
var contentRect = CGRect.zero
for view in contentView.subviews {
contentRect = contentRect.union(view.frame)
}
contentRect.size = CGSize(width: scrollView.contentSize.width, height: contentRect.height + 50)
scrollView.contentSize = contentRect.size
contentView.frame.size = contentRect.size
contentView.layoutIfNeeded()
}
The button I try to reach has a Y value of: 1030.0
The height of the contentView is: 871.0
Step-by-step:
Add a scroll view to your view, background color red, constrain it 20-pts on each side:
Add a UIView as your "content view" to the scroll view (green background to make it easy to see), constrain it 0-pts on each side, constrain equal width and equal height to the scroll view. Important: change the Priority of the Height constraint to 250!
Add a UILabel to the contentView, constrain it 30-pts from the top, centered horizontally:
Add another label to the contentView, constrain it 300-pts from the first label, centered horizontally:
Add a UIButton to the contentView, constrain it 30-pts from the bottom, centered horizontally:
Now add a vertical constraint from the bottom of the second label to the top of the button, and set it to 400-pts:
As you see, this pushes the button off-screen past the bottom of the scroll view.
Run the app. You will be able to scroll down to see the button, and you'll be able to tap it.
Absolutely no code needed!
If you use AutoLayout, you don't needed to install frames manually.
You can try install constraints properly and content size will be right in this case and you won't have to install content size manually.
If you achieve this, I guess everything will work correctly.
You can follow this or this guide

Map view getting shrunk in Dynamic table view cell

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!

Header View behind NavigationBar [duplicate]

I have a UICollectionView that is the entire view but it lives inside "view" (it is not UICollectionViewController). Adding a cell to this collection view shows it in the following order in storyboard:
This is how it looks in the emulator:
I don't understand how to get rid of that view. All the insets are at zero in Storyboard Size Inspector for collection view. Just to be sure, I also have the following code:
override func viewWillLayoutSubviews() {
let layout = self.collectionViewProducts.collectionViewLayout as! UICollectionViewFlowLayout
let containerWidth = UIScreen.main.bounds.size.width - 40.0
let itemWidth = (containerWidth / 3.0)
let itemHeight = (itemWidth / 0.75) + 30
layout.sectionInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
}
How can I get rid of that top padding?
You can fix top padding issue by considering one of the following method.
Method 1: Natural way to fix your problem by setting up your collectionView dimensions properly from StoryBoard.
Method 2: **Updated**
You can validate collection frame in viewDidLayoutSubviews or viewWillLayoutSubviews
override func viewDidLayoutSubviews() {
collectionView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
}
Method 3: You can Adjust Scroll View Insets from your StoryBoard Attributes Inspector.
Method 4: You can fix this issue programatically by adjusting CollectionView contentInset.
collectionView.contentInset = UIEdgeInsets(top: **Any Value**, left: 0, bottom: 0, right: 0)
Output with top padding 5:
Output with top padding 44:
Output with top padding 64:
I think because this viewController is embedded in a navigationController. Let select this viewController in the storyboard and uncheck Adjust Scroll View Insets:
There is one more way to resolve this issue and that is selecting collectionView -> scrollView Content Insets -> "Automatic" to "Never".
By default scrollView Content Insets value is Automatic. Please check the below image.
For more details check: UIScrollView.ContentInsetAdjustmentBehavior
https://developer.apple.com/documentation/uikit/uiscrollview/2902261-contentinsetadjustmentbehavior
Make your Navigation Controller > NavigationBar translucent by unchecking the Translucent check box in IB > Attribute Inspector, and it will work.
I also had the same problem, and i fixed it with a way totally ridiculous solution.
My collectionView contained several sections which had no title & no item cells.
The top, bottom inset values of the section insets were 10 respectively.
so each empty section charged height of 20 pixels.
I had 4 empty sections, and therefore, 80 top margins in the collection view.
Hope you check this as well if none of the above solutions works.

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.