Unable to move cell up in uicollection view from storyboard? - ios

I have added UICollectionView.I have also added the Cell in the UICollectionView.The issue is cell always getting space from top of 74.On interface builder the y space column always shows the disabled value please tell how can i remove such issue?

Any given cell's position is determined by the collection view. You can adjust the cell's insets to increase spacing between cells and the scroll view's insets to position all of the cells in the scroll view, you can't directly manipulate a cell's position out of the box. When noting the position of your first cell you should also take into account whether your viewController has extend edges under top bars on and whether it has adjust scroll view insets on.

Related

How to make UICollectionViewCells with content lined up across cells

I have a UICollectionView that is using flow layout and it is horizontally scrolling. In each cell, I have five subviews (labels and image views). I have an array of model objects, one object per cell, that contains the data to be displayed in each subview in each cell.
Some items can have more content than others (some labels will have more text), but I want all the cells to be the same height. I also want it so that the top of each subview is lined up with the tops of the same subview in the other cells. And if all model objects have a nil value for one of the fields, then the subviews for that field should be left out of each cell.
For example, I have the following image where it shows three cells and the subviews in each cell. For this example, all of the cells have data for subview 1, so it shows up in all of them. But only the first two cells have data for subview 2, so it only shows up in those cell. Likewise, subview 3 only shows up in the first cell because that is the only cell that has data for that subview. Now for subview 4, none of the cells have data for it, so it is completely skipped and there is no space taken up in any cell to render it. Instead, subview 5 is rendered in the place where subview 4 would normally have been rendered and it is only in the third cell because only that cell had data for it.
I also have dashed lines between the subviews showing that they are supposed to be lined up across cells.
Question
So I am just curious what the best way to do something like this is, given these requirements. Could this be done with AutoLayout or do I need to manually layout these subviews?
The only way I can think of doing this currently is to iterate through all of the model objects, try to figure out how tall each subview is and essentially keep track of the tallest height for each subview. And if no cell has data for a particular subview, the height of that one would be zero.
Next, I would turn those heights into computed offsets for each subview, where the computed offset would be the sum of the heights of the subviews above it and the spacing between each subview.
Then, I would try to lay these out manually, I suppose, with the frame of each subview being set to some width (not sure how to calculate the optimal width of each subview) and a vertical position equal to the computed offset of that subview. Or I guess I could do AutoLayout as well where the top constraint on a field is equal to the computed offset for each subview, but is that less efficient?
And then I guess the height of each cell would be just the computed offset of the last item plus its height plus the spacing to the bottom of the cell. So then I would take the max height of all the cells and set all of the cells to the same height.
But I was just curious if there was a better way of doing this or if there was anything that I was overlooking that could simplify things. I would also like this to be as efficient as possible as the collection view could have up to 100 items in it.
You will need to loop through your data, calculating the "max height" for each item.
I'd suggest putting your 5 "views" in a vertical stack view.
As you loop through your data, get the max height for each of the 5 views. Then, when you set your data in each cell, set the height constraint on each of the 5 views (or, set a view hidden if it is nil in all data items).
Then set your collection view's height to the necessary height to fit all the views plus spacing.

Layout errors when pinning UIView edges to UITableViewCell contentView

Got a problem here which is driving me crazy.
In a storyboard I have a table view in a layout and I'm trying to pin the leading, top, trailing and bottom constraints of a UIView to the prototype cell's content view. Straightforward enough.
Whenever I do this I get these layout errors for the UIView in Xcode:
"Need constraints for: X position, width"
"Need constraints for : Y position, height"
Constraints on that view (Superview is contentView):
In the storyboard I have the following set on the table view:
Row Height: automatic
Estimate (row height): 50
And on the table view cell:
Row Height: 50
I've seen a few SO posts and other walkthroughs that state that pinning the edges is what to do, and as I understand Auto Layout, I've provided the necessary constraints and info.
How can I fix this?
(BTW: I'm running Xcode 11.3.1.)
Update: The cells of this table view will be fixed height. I'm looking for the cell to provide the height for its content.
UIView by itself has no intrinsicSize - so you are telling the cell's contentView to pin to a frame of 0 x 0.
If you are going to add content to the UIView, set the constraints on the content to control the size of the view and the errors will go away.
In the meantime, give the view a height constraint and that will resolve the issue.
Edit
For fixed-height cells... try starting fresh.
Add a table view - give it desired constraints
Add a table view cell prototype
Add a UIView to that cell (give it a background color to make it easy to see)
Constrain the view at Zero on all 4 sides (constrain to margins)
Set the cell Row Height to 50 (un-check Automatic)
Here's how it looks for me:
It turns out I had the table view and cell constraints and settings configured properly; the problem was further up the view hierarchy.
The table view was placed in a vertical stack view, and that stack view's Alignment property was set to center (for the benefit of other views in the stack). This was causing issues for Auto Layout in calculating the width of the table view.
The solution here was to add an Equal Widths constraint from the table view to the stack view. Et voila! The errors on the UIView within the table view cell's content view disappeared.

TableView horizontal scrolling according to cell width

I have table view inside scroll view which will scroll according to table view cell width.I have set the scroll view from top,bottom,leading and trailing.Then I have given the same constraints to view inside the scroll view including centerX and centerY.Then for horizontal scrolling I have set the trailing and centerX priority to 250.But my table view is not scrolling.I want to set it through autolayout.
I want the table to scroll horizontally if label increases inside the table view cell.
This is my view hierarchy
#Zahid I made a public example on github with what I wrote about below:
https://github.com/amichnia/stack-54735700
Original answer:
As it was already said, table views do not scroll horizontally in general. But no one said you can't put table view inside UIScrollView that scrolls horizontally. There would be some challenges here though:
I don't think autolayout with autoresizing cells would work good.
You need to figure out scrollview/tableview gesture recognizers order. You can use UIGestureRecognizer delegate methods to resolve their collisions
You need to determine width of the table view. You can do it with old nasty way:
compute size of every cell first,
take max of these sizes
set it to table view width
reload data
Is it worth the hassle? That's your call to take.
With your setup (I see scroll view as root view) the simplest (but not best) option would be to disable scrolling on table view, enable scrolling both directions on scroll view, and just update whole table view size. It will work well only with small amount of cells though!

How do I change cell height according to its content?

I have a Detail View Controller, which has different text lengths according to which cell you press in the Main View Controller, of which it segues to. How do I set the cell's height according to how much content is in it?
FYI, the cell consists of 3 labels and 1 image view. It is vertically set as UILabel, UIImageView, UILabel, UILabel.
I am using Swift.
This is what happens when I add the auto-layout constraints:
The way to do it is to use auto-layout. You need to set up vertical constraints from the top to the bottom of your cell:
Distance between the top edge of the cell and the top edge of the first label.
Distance from the bottom edge of the first label to the top edge of the image view.
Distance from the bottom edge of the image view to the top edge of the second label.
and so on ...
Auto-layout will take care of sizing the cell depending on the size of the content you put in your labels and the image view.
You should also remember to set the estimatedRowHeight property on your table view to some meaningful value. This will help the table view defer some of the calculations of the content size to when the user starts to scroll.
Also, set the rowHeight property on your table view to UITableViewAutomaticDimension.
There are 4 things you'll need to do:
Set your tableView:heightForRowAtIndexPath method to return UITableViewAutomaticDimension.
Set your tableView:estimatedHeightForRowAtIndexPath to return the average height of the cells. You can set it to something static like 100.
Set your Auto Layout constraints so that all subviews are tied to each other vertically, and the top-most and bottom-most views are tied up to the cell's contentView.
Set the label's preferredMaxLayoutWidth property either in code or in your Storyboard to be equal to the label's width.
To fix this, I added the sizeToFit() property to my label.

Autosizing UITableViewCell: change height of a view inside a custom cell

I searched a lot for a solution but can't find anything to solve this particular case.
I created a custom cell in a UITableView, with these elements in order from top with the constraints:
imageview (constraints top screen, fixed width, fixed height, centered horizontal)
top-label (constraints top imageview, fixed width, centered horizontal) Line set to 0.
view (constraints top label, fixed width, centered horizontal) The height is set to 0.
button (constraints top view, fixed width, fixed height, centered horizontal)
bottom-label (constraints top view, constraints bottom screen, fixed width, fixed height, centered horizontal)
The cell is autosized in height correctly, also if I insert a text for top-label very long.
Now I want to attach an action to the button to enable the resize in height of the view, like an accordion. So I'm trying to change the height with no success, anything change also if I reload the tableview.
I tried to set a constraints to the height of the view, but If I change that all the content move but the height of the cell doesn't change.
The only way I can have the view changing the height is setting the the rowheight of the table, but that change all the cells of the table and I want to open the view of only one cell.
Is there another way to do that?
Thanks in advance
Ale
EDIT:
For clearance I've solved using the delegate method suggested by noobular, setting the view to 0 height permit to have that expanded when the height of the cell change.
I achieved a similar effect by these methods. First, I determine a constraint that will represent the height of my cell. I'm using the height constraint of a decorative bar on the left hand side that's pinned to the top and bottom of the cell. It's important that this constraint belong to an object that is a child of the cell's content view and that will be on-screen in both open and closed states. This constraint is connected to a property on the cell it will control.
When I want to change the height of the cell, i.e. to 100, I can do so as follows:
cell.heightConstraint.constant = 100.0
cell.layoutIfNeeded()
tableView.beginUpdates()
tableView.endUpdates()
The calls to beginUpdates and endUpdates force the tableView to layout its cells again, thus expanding or contracting the cell. Note that I'm actually calling the first two lines in the cell's own setSelected:animated: function and the remaining two in the table view controller's tableView:didSelectCellAtIndexPath: method. This has the effect of expanding a cell when it is selected.
Edit: For auto sizing cells where you want UIKit to automatically calculate the height, I would suggest researching UITableViewAutomaticDimension
Implement this method for your tableView delegate:
tableView:heightForRowAtIndexPath:

Resources