Custom UIView resize according to content - ios

I have a UITableview which needs to displays content(Image and text) based on the size of the image. (full width - height according to aspect ratio)
I have defined a custom cell and embedded Customview inside it.
Customview(It has imageview constrained to fit the Customview).
CustomView receives the image size from api. I calculate the height needed for Customview.
I have tried setting intrinsicContentSize of Customview according to size received of the image
As well as setting height constraint of the image.
But Height of the container does not reflect in UITableViewAutomaticDimension tableview.
However if set the height of the CustomView inside the cell it works.
Whats the best way to accomplish a custom view which signals the superview the height it needs to display?
Eg - http://ioscake.com/proper-usage-of-intrinsiccontentsize-and-sizethatfits-on-uiview-subclass-with-autolayout.html

Related

iOS custom view inside table view cell with intrinsic height

When UILabel with multi-line and wrap-word enabled is used on UITableViewCell, UITableView handles the different cell heights without any problem. UILabel calculates its height according to its content and its width.
I implemented a custom view (https://github.com/fthdgn/ios-demo/blob/master/demo/CustomGridView.swift) which also calculates its height according to its content and its width. This view is a simple grid view which determines row count and height according to column count which is determined by available width. However, there are problems when custom view is used on UITableViewCell.
On my demo application (https://github.com/fthdgn/ios-demo),
On the 1st tab [1st image], the custom view is used alone. Its height is correct. Button adds new items and updates it height correctly.
On the 2nd tab [2nd image], the height of the custom view is calculated wrongly. This height is calculated according to placeholder width of the view on the XIB file. This width is smaller than correct width, thus the height is longer than correct value.
Reload button on the 2nd tab [3rd image], reloads table. Custom view calculates and updates itself correctly.
On the 3rd tab [4th image], UILabel calculates its height according to available width from the start.
Why does not my custom view work like UILabel? Are there missing setNeedsLayout, invalidateIntrinsicContentSize calls?
I updated the code to show only 1 row with 11 cells on UITableView.
The width of Grid View on on XIB file is 300, because the selected device is 4S. The simulator I use is 11 Pro Max, it is wider than 4S.
intrinsicContentSize and layoutSubviews call prints these:
content (-1.0, 50.0) frame (300.0, 0.5)
First calls intrinsicContentSize. Frame is what is on the XIB,
constraints does not resized the view yet. This width requires 50
height to place cells.
layout (394.0, 50.333333333333336)
Calls layout, the height is determined according to intrinsicContentSize, width determined by UITableViewCell constraints. It is wider then the value which intrinsicContentSize is calculated by.
content (-1.0, 20.0) frame (394.0, 50.333333333333336)
Because layout changed, I invalidate the intrinsicContentSize. New intrinsicContentSize value is calculated according to correct width. New height is 20.
layout (394.0, 50.333333333333336)
Something triggers layout. Eventhough last intrinsicContentSize.height is 20, it still uses 50.
content (-1.0, 20.0) frame (394.0, 50.333333333333336)
Layout invalidates intrinsicContentSize. intrinsicContentSize is recalculated but its value does not change.
After these calls, event though the last intrinsicContentSize wants 20 height, frame continues to be 50. You can see at 2nd screenshot, the row with 11 cell is longer than what it should be.

Set subview height relative to parent view in Storyboard

I have a UIImageView in a UIViewController's view. What I want is to set it's height 1/3rd that of the view no matter what the screen size is. I of course know how to do this from code. But how to accomplish this using Storyboard/Interface builder only ?
Add a height constraint whose multiplier value is 1/3 so the image view's height will be one third of its superview's height.
--
Here is how it looks with different device sizes:

How to achieve resizing of UITableView cells like instagram?

I need to be able to resize an UIImageView according to the size of the image which is in aspect fit.
and also i need to be able to resize the cell that the image is contained in.
A example would be the instagram app, they have different size images yet the cells are sized appropriately?
Use auto layout when creating your table view cells
Set the table view rowHeight to equalUITableViewAutomaticDimension.
Set theestimatedRowHeight or implement the height estimation delegate method.
You need to adopt the dynamic cell height. Now that cell will automatically determine its height from its content view subviews, make sure you have set your auto layout constraints carefully. Set content mode of UIImageView to aspect fit.
Below are some links to follow:
1. Rey wenderlich
Site Point

How to implements a custom view compatible with AutoLayout that has dynamic height?

I want to implements a customize, and the view's height can be calculated according to it's content and width. The view's width is arbitrary and in IB usually set leading and trailing offset. The question is how can I get the width of this view and do the layout?
A similar view is UILabel, if set numberOfLines property to 0 it can adjust it's height according to content and width. UILabel has a property preferredMaxLayoutWidth, during constraints update, this property is set to correct width and using these do the work. And the same, when and how this property is set?
For example, i have a grid image which look like this
If width is given, then we can calculate the height of the view. And I have pin it to a cell like this
So the cell's height will adjust according by the grid image view's height.
But how did i know the exact width of the grid image view? The actually result is following when the table view first shown.

Prevent resize when setting UIImage in UIImageView

I have written some auto layout code programmatically. I have successfully achieved the following layout (without an image in the image view):
There are two views, an image view followed by a single line label
The label is filled with text
The image view height and width is constrained to the label's height
The label's height is the "intrinsic height"
Everything works perfect.
When I programmatically, in response to some events, set an image in the image view, the image view is resized to fit the image, and this causes the label's height to change to match the image view, ignoring the intrinsic height of the label calculated by the text in the label.
I set the height of the label using this constraint:
self.subjectLabel.autoSetContentCompressionResistancePriorityForAxis(ALAxis.Vertical);
Maybe there is something wrong with that? Does anyone know where I have gone wrong?
I ended up working around this by resizing the UIImage using the ideas from this page: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way.
I resized the image during layoutSubviews to ensure that the frames for all the subviews has been calculated.

Resources