I have a UITableView with various cells and I want to insert lateral space (to the right and to the left) between the cell and the View, but I am not able to do that.
Can anyone help me with this?
You should use a UICollectionView instead.
With UICollectionView, you can set the Cell's Width and Height, and add custom spacing between each cells. You can also configure the space between the cell and the border of the CollectionView.
If you really need a UITableView, try to add a Leading Space To Container Margin and a Trailing Space To Container Margin contraint between your UITableView and its Superview.
Here is an example : my tableview has a grey background, my cell has a yellow background and the superview has a blue background. Thank to the two constraints, there is a space on the left and the right of my cell
Is that what you need ?
One way is to make the TableView's width shorter that the controller's view width and add constraints to keep the space exactly as you want it.
Another way is to design your cell with an "internal container" with spacings to edge of the cell's content view.
You can easily do this in a storyboard using a container view…
Add a container view to your view controller, set the left and right constraints to the inset you need, add a UITableViewController and then drag from the container to the table view controller and embed it
Related
I have got a UITableView with dynamic cells.
In each cell will be a small textview on the right side of the cell.
How can i set an constraint, which fixed this textview with a fix value of space to the right side of the cell in iOS8 Swift?
At the moment I only know, how I can set the space from textview to the left side of the cell, but I need to fix the space between textview and right side.
You can create a custom TableViewCell or in your prototype add the textview and manipulate the constraints of XIB file in storyboard. What you can do is set for example the constraints of width and height and also set the Spacing to the right side and the top side.
Or, another thing you can do is: choose the textView and then press ctrl+LeftMouseClick and drug to the left into the cellView.Like this:
When you let the click choose "Center Vertically in Container" like this:
To "Center Horizontally in Container" do the same thing as above, but this time drug to the top, int o the container cellView again.
I'm having a strange issue with my tableview, debugging the view hierarchy i'm getting this
I've tried a lot of constraints with no result, how should I setup my tableview to avoid that kind of misplacement ?
Edit
I've a custom cell with an image view, actually set just this 5 constraints
Top, bottom, leading and trailing values are 0
You just need to set Your UITableView constraint to Leading,Trailing, Top and Bottom. and also for your UIImageView set same constraint. so your imageview will show fit to cell as per table width. no need to set Align Center X constraint.
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:
I've added a label inside a tableviewcell (Prototype cell) and using storyboard I've applied the following constraints on the label:
50 leading margin
50 trailing margin
20 top margin
20 bottom margin
In landscape mode(iPad), the label appears correctly.
In portrait mode my label does not resize like I would expect it to do and as you can see below the label width is longer than the screen size.
Any help would be appreciated.
Figured it out :)
Here's the problem with tableviews in general.
When you drag a tableview onto a View it resizes itself quickly in storyboard to fill the view container completely. So, like most people there didn't seem to be an easy way to apply the constraints on the tableview.
So, this time I dragged the tableview a bit so that I could see the bounds.
Here's how I was able to see the bounds between the tableview and right margin.
Here's how I moved the tableview so that I could see some separation between the left margin of the tableview and the containerview.
Once I was able to see the separation between the tableview and it's containerview I applied the following bounds between them like this:
trailing margin : 1
leading margin : 1
top Space : 1
bottom space : 1
After this point when my view resized from portrait to landscape so did my tableview, and so did my labels.
Programming is cool :)
Try to use Autosizing and select the right bar. Sorry I can't post an image for more help
I have a collection view which displays cells looking like a classic table view with image, title and subtitle.
I want to be able to switch the layout to a grid showing only the images, 3 in a row.
The content of the collection view cell is layouted in a storyboard with auto layout. The imageView has the following constraints:
Leading 0 to the left cell edge
Top 0 to the top cell edge
Bottom 0 to the bottom cell edge
Fixed width
Horizontal space to the labels
After the layout change the imageView should have constraints of zero to all edges of the container, so that it fills it completely.
At the moment when I change the layout the app crashes with Unable to simultaneously satisfy constraints.. What is the best way to fix the constraints when the layout is changed, maybe even replace the cell class?
Turns out the problem was in some other part of the code.
You can change the collectionView layout with setCollectionViewLayout:animated:completion: and reload the visible cells in the completion block.