Dynamic height of UITableViewCell and UIImageView inside - ios

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.

Related

UITableViewCell change on height constraint and autolayout

I have three two subviews inside cell contentview. I have height constraint for both of the subviews. Depending on flag I want to hide either of the view. For this i have changed height constraint.
It is not changing the cell height. How can I change the cell height.

Make UIScrollView with dynamic height subviews work automatically with Auto Layout

I have the following view structure with its constraints:
UIView "parent"
UIScrollView (leading, trailing, top and bottom to superview)
- UIView "container" (leading, trailing, top and bottom to UIScrollView,
equal width and height so parent UIView)
- UIView "A" (leading, trailing, top to UIScrollView, height of 200)
- UIView "B" (top, leading and trailing to UIView A, height of 140)
- UIView "C" (top, leading and trailing to UIView B, height >= 88 "rest
of the screen until bottom", bottom to UIView "container")
"A" and "B" UIView's do not change its size but "C" does. Inside it, I am adding programmatically n "labels containers" UIView that have different heights depending on the content of m UILabel that they host.
Right now, I am calculating the size of the n UILabel with boundingRectWithSize: and I am sizing the height of their parent "labels containers" UIView that it is being added inside "C" UIView setting its height constraint to the sum of all UILabel.
Then, I resize "C" UIView height constraint so that it is equal to the sum of all added UIView.
This is working perfectly on all different screen sizes, portrait and landscape. The UIScrollView is showing all three "A", "B" and "C" subviews, having "C" n UIView that host m UILabel.
But now I am having troubles when rotating the device. I face the problem that I have to recalculate the size of all UILabel to change the height constraint of all the "labels container" UIView and change the "C" UIView height constraint so that it can fit everything without large blank spaces between all views.
So my question is: how can I achieve the same behaviour using exclusively Auto Layout?
Now I have to recalculate sizes and change height constraints so that everything adapts, but I would love that all UILabel resize themselves automatically and fit their content, then the "labels container" UIView resize to fit all the UILabel and then "C" UIView resizes automatically to fit the content.
Thank you in advance!
As per my knowledge you need to add TableView for your dynamic labels and to achieve this you have to follow below steps:
1step : Add ScrollView in your ViewController.
2step : Add ContainerView in ScrollView to manage scroll constrains.
3step : Add your "A" view with fixed size lets say 150px.
4step : Add your "B" view with fixed size lets say 150px.
5step : Add TableView for dynamic labels with fixed size 0px. At this step make Outlet of your tableView height Constrains we will use it in next step.
6step : Call your service and add labels in your tableView.
in this 6th step while you are adding labels in your table view means while you reload your tableView do below code for dynamic heigh constrains.
7step : Now as per your Row height adjust height of table view as mention below.
arrListArray = #[#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine",#"Ten"];
int rowHeight = 44;
self.tblHeightConstraint.constant = (rowHeight * arrListArray.count);
now reload your constrains with below method:
[UIView animateWithDuration:0.1
animations:^{
// Called on parent view
[self.view layoutIfNeeded];
}];
it will increase your scrollView contain size automatically.
Show sample images :
View Constrains Stack :
Now as per your comment you have to add CollectionView in place of tableView and add TableView in CollectionViewCell with your specific design.
Hope this will helps!
Ok, I was doing three things wrong:
1st: I was calculating the height of the labels to set then the size of the "labels container" UIView using a height constraint. Height constraints not needed as Auto Layout manages everything.
2nd: I was modifying the "C" UIView constraint height manually by adding the height value of each "labels container" UIView added.
3rd: After fixing the first two steps, I forgot to set the last added UIView constraint with its parent view...
Now everything works as expected.

Can't set UILabel width proportional with UITableViewCell width in storyboard by Autolayout in iOS

I have a simple tableview cell with one label (named Bio).
Tableview in storyboard. I want that this label should be change in width according to cell width. e.g. 40% of cell width.
It's easy to do in any view. But can't find the option when I control-drag label to cell content view. The "Equal Widths" is missing. (Also "Equal Heights" missing). Is anything I am missing or its XCode(6.1.1) bug ??

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.

keep TextLabel for tableviewcell at top

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.

Resources