Resize UITextView inside a UITableViewCell with iOS Autolayout - ios

My Goal
I'm creating a custom UITableViewCell (MessageCell), and I'd wish to have my UITextView resizing both in width and height whenever the cell changes its size, while keeping some basic constraint like margins to the edges.
Note : I also change my cell's height in the heightForRowAtIndexPath method depending on the content which will be placed into UITextView.
What I tried
Here are all the constraints currently applied & the cell :
I tried :
With a UILabel.
To fix a Vertical Space from the bottom, thinking it would change the height to fit all the constraints.
To override Height with an User Constraint Height >= 43 (my initial height), but the purple-automatic Height is re-created again whenever I do this.
To find a solution on SO first, but didn't find a case like (even if the UITextView's height's resize with autolayout seems to be a frequent issue).
Plenty of combinations of various & random constraints until my fingers bleds.
How it render now
So...
If someone has any clue or guideline to achieve my goal, I'd appreciate!
I might add, I'm totally new to Autolayout & constraints. My reference : Ray Wenderlich's awesomeness

You need to get rid of that height constraint that the text view has. Make sure you have constraints to something above and below the text view (usually the top and bottom of the cell), and then you should be able to delete that height constraint.

Related

Swift how can I get UIScrollView dynamic height

I googled but have not found working solution, I have UIScrollView adding inside UITextField and UIButtons with NSLayoutConstraint, I don't use storyboard and setting everything programmatically
my issue is: can't calculate dynamic height of UIScrollView.contentSize because I am using AutoLayout
also tryed UIScrollView.layoutIfNeeded() but no luck
can any one guide or advice me how to to get it work?
#DonMag's comment was right solution to my problem
stackoverflow.com/a/44933358/6257435
if you have in UIScrollView objects with AutoLayout don't forget add .bottomAnchor to last item
Sum of scrollview subview’s height and constraint constants is the final height. Even after constraints layed out, it’s always summ. So just calculate it, and you height. But why do you need calculating scrollview’s content height? If it’s adjusting wrong, set your subviews height constraints, top and bottom contraints to scrollview, and content size will be calculated automatically

UICollectionViewCell inside UITableView Cell and Auto Layout

I have anUICollectionView inside aUITableViewCell and I have some issues regarding to Auto layout.
That's the hierarchy of my views.
UITableViewCell
|
-------ContentView
|
-------MyView
|
|-----UICollectionViewCell
Until here, all views are pinned up in his superviews., e.g: trailing space=0, leading space=0, top space=0, bottom space 0.
The height of myUICollectionViewCell is dynamic, based on the number of itens, which I draw usingUICollectionViewLayout. So it has a height constraint which is >= 100.
After the Items are in place, I call one delegate to update that height constraint, so myUITableView is able to calculate the right height for theUITableViewCell.
Even though this are working fine, sometimes I get the auto layout error above.
How can I avoid this?
The cell's contentView has a fixed height (44). That's causing the occasional conflict between the storyboard, and Auto Layout's derived height.
Self-sizing is the best way to go. What you want to do is to get your "collection view" to determine its intrinsic size, so Auto Layout can automatically handle the variable cell height for you.
See the detailed walkthrough by smileyborg in his answer to Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.
He uses labels but the principle is the same. The cell asks its contentView to lay itself out. The contentView asks the label to lay itself out. The label determines its height. Auto Layout determines the contentView's height, based on the label's constraints, and the cell determines its height, based on the contentView's height. The point is that the label doesn't have a height constraint, and your "collection View" would no longer need one either.
Once you adopt self-sizing, it eliminates a lot of code (and size constraints), as containers manage their size, and constraints simply handle spacing.

Working with AutoLayout using UITableViewCell

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.

Resizing a UITextView in UIScrollView using Auto Layout issue

I really hate to ask here because I usually try to figure things out on my own. But on this one I've stuck for days and can't find a solution anywhere online.
I have a ScrollView containing multiple subviews. I've got an image view and two labels at the top with fixed heights. Then there is a UITextView and another ImageView (see pictures).
I add the text to the text view programmatically so it should have a dynamic height and the ImageView should move to the bottom so you can scroll. I don't want the TextView to be scrollable in itself but I want all the subviews to move as well.
I know I should be able to solve this issue using constraints. But I feel like I've tried everything and nothing worked yet. It worked when I disabled auto layout and moved the views manually. I'm wondering if there is a better way though.
As you can see I pinned the TextView to the ImageView above with a 1,000 priority and to the ImageView below with a 1,000 priority. The height constraint can not be deleted so I set it to the lowest possible priority. The ImageView on the bottom is pinned to the bottom of the superview with an absolute height. Its height constraint also has low priority. (I can post an image of the ImageView's constraints, if it helps)
I also tried adapting the frame programmatically but this is not working well in combination with auto layout. (If it helps I can of course post the code)
What am I doing wrong? Shall I just disable auto layout and do it manually? This seems unclean to me. Is it even possible to do?
I really appreciate your help :)
Greets,
Jan
Make sure the Scrolling Enabled attribute on the UITextView is unchecked in Interface Builder. I believe that the Auto Layout system takes that into account when it calculates the intrinsic content size.
If somebody is struggling with a similar problem: This is what I ended up doing:
Remove all subviews from the ScrollView in IB
Programmatically add a single UIView to the ScrollView.
Add all the views to the UIView as subviews (move them using setFrame)
Set the Frame of the UIView appropriately to the subviews
Set the ScrollView's contentSize to the size of the UIView.
A little more work but it finally works. This follows Apple's mixed approach guidelines that can be seen here (look for UIScrollView): http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/index.html
The problem is the height setting. You somehow have to try to delete it. If you have added other constraints that are "sufficient", it should become deletable.
At the moment you have one user constraint for the height that is "Greater or equal" and an "Equals" constraint as well. Clearly, those are not working well together.
Maybe there is a conceptual error as well. The lower image view should not be fixed in position, so the distance to the lower image view will not be a "sufficient" constraint to let you delete the fixed height.
I think it should work if
the lower image view has a fixed height and
a fixed distance to the text view above, and
the text view has a minimum height as well as
a fixed distance to the image view above
(which should be fixed in relation to the superview).

Set height constraint for UITextView

I'm fairly new to iOS development and am having a hard time sizing a UITextView to it's content. I've found several examples of calculating a strings render size and/or updating the frame size of a textview, however I can't seem to get those to work.
When I add a UITextView to my view it adds a Height constraint which I think is locking it to a specific height. How can I work around it, update the constraint via code, and/or get my UITextView to resize vertically to show my content?
You can create an outlet to the vertical size constraint, and then adjust the .constant property of the constraint to amend the height of your text view.
A UITextView inherits from UIScrollView. As part of it's logic, it changes the height of its contentSize property to allow the user to scroll vertically to any portion of the TextView's content.
I would recommend using the ContentSize property of your UITextView to figure out how "tall" your content really is. You can then set the frame of your UITextView accordingly.
OR: You could figure out the size of your text using the right method from the NSString+UIKitAdditions category.
If you don't want autolayout then disable it in the xib file.
If you want to change the contraint and it's value this tutorial will help you to deal with autolayout.

Resources