I have a UITableView below that has a custom uitableview cell. I am trying to get the two labels inside it do be 50% in width and have the left cell have right aligned text and the right cell to have left aligned. Any clue why this isnt working correctly below?
Use auto-layout, and set both the labels an equal width constraint, it should do the trick.
Edit:
Without auto-layout you can try something like that:
Related
Question: How can I use constraints in a UITableViewCell using UITableViewAutomaticDimension when there are labels side by side which can each contain a different number of lines?
I have a table with cells that contain labels side by side:
I'm having a hard time getting the cell to size properly if one of the labels on one side have more lines than the corresponding label on the other side. One of the labels always gets cut off. If both labels have the same number of lines, the cell resizes perfectly and all text can be seen.
I've tried a number of constraint tweaks but nothing is working perfectly as desired.
I've tried adding hidden label for height...but it doesn't work well for the cell with 4 labels
I've tried placing the labels in a view but then the height of the view still needs to be calculated.
Currently the constraints are setup normally: top, bottom, left , right, labels are set to 0 lines etc and works great when both labels have the same text or same number of lines.
Is there any way I can do this with constraints? Or do I need to revert to the old height for row at index path? =(
There is a way to achieve what you want to do just using constraints. The trick is to set the relation of the constraints between the two lower labels and the bottom to Greater Than or Equal instead of equal.
Here is how I set the constraints:
Which has this result:
Or you could set the constraints between the labels top and bottom to Greater Than or Equal and leave the bottom constraint at equal:
To get this result:
I found a solution where I did not have to revert to using tableView:heightForRowAtIndexPath.
I added a label to the cell with the text color set to clearColor and called it 'heightLabel'. I added constraints to the heightLabel so the cell would resize based on the heightLabel's constraints.
Then whichever of the left or right labels had the most text, I placed that label's text inside the heightLabel.
The cells still were not resizing and I had to call 'layoutIfNeeded' on the cell before returning the cell in tableView:cellForRowAtIndexPath.
The heightLabel constraints are now what is being used to resize the cell and UITableViewAutomaticDimensions is working properly.
This may not be the best solution but it's working out pretty well. If there is a more proper way of doing this I'm open to suggestions.
I have an application in which I want to have a tableView with custom tableViewCell that has five columns.
I want them to be like 20%, 30%, 15%, 20% and 15% of the whole cell's width, because I want my application to launch on iPhone and iPad.
What is the best way to do this?
Now I have created a tableViewCell prototype with 5 labels. I also added constraints to them but the width constraint is just a constant. Also when I added width constraints to labels, some collisions appeared there and I don't know how to fix them.
Here is a screenshot.
You can create table view cell with collection view. By that, you can get different part in cell as well as you can change each column width programmatically based on your needs.
Thanks
A table view has only one column and allows vertical scrolling only. link
If you want to use a few columns, you can use UICollectionViewController
I also added constraints to them but the width constraint is just a constant. Also when I added width constraints to labels, some collisions appeared there and I don't know how to fix them
To fix it change width constraints to be a multiplier of superview width.
I am making filling a UITableView with custom cells of dynamic heights. I am using the methodology as described here.
For the purposes of simplicity, say my cell contains 2 UILabels as shown below.
The purple UILabel will remain the same height for all cells in the UITableView. In this case, the purple UILabel is taller than the green UILabel. I would like the constraint between the bottom of the purple UILabel and the bottom of the contentView to be 8.
However, there is a second scenario that is possible as shown below.
In this case, the purple UILabel is still the same size as the first case (even though it looks smaller, but since the overall cell height is larger, relatively, it looks smaller), however the green UILabel is much larger. In this case, I would like the constraint between the bottom of the green UILabel and the bottom of the contentView to be 8.
Now my dilemma is that I can set either one or the other (I am using storyboard). But I cannot think of a way to make both possible at the same time. My guess is that you would need to adjust constraints programmatically during runtime depending on the size of the green UILabel.
Any thoughts?
I am using XCode 6.1.1 in Swift.
Thank you.
If the purple label is always the same height, then you should just set the height constraint of the element, rather than setting its bottom constraint to superview as 8.
For the green element, perhaps you could select both that element and the contentview and set them as having equal heights from the "add new constraint" pane at the bottom of the storyboard window. Then, select that constraint and edit its multiplier value in the attributes inspector to some value less than 1. That would make the element's height always be some percentage of the parent view's height. You could tweak that around until it gets you close to the desired 8px from the view below it.
I am working with autoLayout, I want to place a UIlabel on UITableViewCell, which should be always at the right of the cell and the center of right.
So here is what I want to achieve
So here you can see label named RC which I am talking about.
So here my cell height changes dynamically, so whatever be the text height I always want to be the center right.
2) Now as the cell height depends on the text of the label sentence. I want to make sure that if the cell has the default height and it will not decrease below it. It can increase to any height.
Select your label go to editor then select vertical center in container, also fix it using the trailing space from super view and set fit size to content and it should work. For dynamic tableViewCell height created using autolayout find the TUTORIAL. This will help you a lot.
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