keep TextLabel for tableviewcell at top - ios

I am using UITableView in my project. i am increasing and decreasing the height of UITableView cell on the continuous taps. In other words i am toggling the height of UITableViewCell on taps. Now i want to keep the UITableViewCell textLabel at the same place which is at the top. But when i increase the height of UITableViewCell the textLabel come in the middle of the cell.
To avoid this is am using sizeToFit for cell.textLabel but it not working... any help...

The easiest way to do this is add Auto-Layout constraints to the default TextLabel programmatically. I would pin it from the leading edge, trailing edge, and bottom edge of the Superview (in this case being the cell).

try
label.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
and don't do the size to fit. What this will do is change the distance from the bottom of your label to the bottom of the cell. In other words, it will keep the distance from the top constant.

Related

Dynamic height of UITableViewCell and UIImageView inside

I have custom UITableViewCell like this:
Inside UITableViewCell I have UICollectionView, aligned at top with constraints, below it self-made Stepper (view with two Buttons and Label inside), aligned at right by constraints and with fixed width. From the left of UITableViewCell and to the leading of stepper there is an UIImageView, aligned by constraints and also with Aspect constraint (1:1 in preview, i.e. height = width). Stepper's bottom constrained to UIImageView bottom, and also at the bottom of UIImageView another view with Segmented Control and two Labels (named Material). Bottom of UITableViewCell constrained to Material's bottom.
I want to change UIImageView height by changing UIImageView Aspect constraint programmatically (by selecting item in UICollectionView at top) and so height of UITableViewCell, as well as the other bindings, are also changed. For example I want to set Aspect constraint to 2:3 or 4:5, 5:7.
I created IBOutlet for my Aspect constraint inside UITableViewCell class, and try to change it constant in collectionView shouldSelectItemAt method but nothing changes.
What am I doing wrong? I'm new to iOS development, please help me to figure out this issue.
Thanks.

Set minimum height of table view cell?

I have a UITableView,In this table i have created as custom cell with image & label.I have given label leading,trailing,top,bottom.I want to set the size of label to increase according to text which is happening.I have used UITableAutomaticDimension & EstimatedRowHeight.Now i want to set minimum height of cell equal to height of image.Now if text is very less in label then height of image is decreased.Now i want to keep height cell minimum as image height & increase it when there is more text.Please help how can i do it?
Looking at the view you shared i would suggest the following constraints.
Top of UIImageView to top of your cell.
Left of UIImageView top left of your cell.
Width and Height constraints of your UIImageView.
Bottom of UIImageView to the bottom of your cell which will be greater than or equal to.
Right of your UIImageView to Left of your UILabel.
Top of your UILabel to top of your cell.
Right of your UILabel to right of your cell.
Bottom of your UILabel to bottom of your cell.
Adding these constraints should solve your problems.

TextField height not automatically updated

I have made a TableView, in which I set
self.tableView.estimatedRowHeight = 120.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
In one cell, I place a view, in which I drag a label, and for that label, I set constraints to top and bottom equal to view top and bottom. I increase the font of the label to 150. The table view cell scales nicely.
In the second cell, I place a view, in which I drag a textfield, and for that textfield, I set constraints to top and bottom equal to view top and bottom. I increase the font of the textfield to 150. The table view cell does not scale nicely, but keeps as small as my estimated row height.
How is this possible? Which properties should I set additionally for the textfield?
I am using iOS 9.0 as Deployment target. Below are a screenshot of the constraints and a screenshot of the simulator how it currently looks like.
Tableview does grow with textfield. You might not be setting correct font family.
In cellForRowAtIndexPath
[cell.yourTextField setFont:[UIFont fontWithName:#"HelveticaNeue" size:120]];
The cell height increased:
Try this and let me know if it worked. I played a lot with it.
Hope this can help.

Need assistance regarding UITableViewCell SeparatorInset

1> I have UITableViewCell and UITableViewCell subclass cell both in my tableview. Setting SeparatorInset on UITableViewCell subclass cell is not working?, but If I set it on UITableViewCell it works fine.
2> Apple doc regarding SeparatorInset says:
You can use this property to add space between the current cell’s
contents and the left and right edges of the table. Positive inset
values move the cell content and cell separator inward and away from
the table edges. Negative values are treated as if the inset is set to
0.
Only the left and right inset values are respected; the top and bottom
inset values are ignored. The value assigned to this property takes
precedence over any default separator insets set on the table view.
a. If I set [cell setSeparatorInset:UIEdgeInsetsMake(0, 100, 0, 0)]; on UITableViewCell it push the content towards right
but setting [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 100)]; on UITableViewCell push only separator towards left not the content, why?
1)
In UITableViewCell you have subviews which position you cannot edit directly (title, accessoryView, ...). Here comes separatorInset which enables you to adjust position of those subviews (in other words separatorInset is considered during position calculation of those subviews).
In custom UITableViewCell subclass you are positioning your own subviews, so unless you specifically consider separatorInset during calculation of their position, it has no effect.
2)
separatorInset does not 'push' the content to left/right, rather adds (virtual) insets to container in which they are positioned. So if e.g. you only have title that does not stretch to full width of the cell (-100) you would not see any difference after adding right inset.

center custom UITableViewCell

I have a custom UITableViewCell subclass with width of 300.
Since the cell is 20pt shorter than the table view, When the cells get loaded, its placed on the left most position, is there a way to center this custom cell to the tableview's center?
I tried cell.center = self.tableView.center in cellForRowAtIndexPath, but it wont work. any idea?
You can pad your custom cell with 10px to the left so it will only appear to be centered, so you just make the width 310 and move everything else inside by 10 px.
a) Resize your UITableView to 300 and center it.
or
b) Make your cell's width same as UITableView's width. Make your custom cell's background color clearColor. Add one more view to your custom cell subclass that will contain every other view and add every view in that view. Think of this like an illusion where the cell is actually wider but it's contents are centered in a fake view.

Resources