Move textview of variable height when keyboard appears - ios

I've a VC with few views above textfield and a button below text view contained inside scroll view. I'm trying to move textview up when keyboard is shown but I can't get it working correctly.
Textview height <=100 in IB. I'm adjusting textview height as we type in it up to max 120 then it scrolls.
I've read it and tried most of solution provided on Move textfield when keyboard appears swift
The problem I'm facing is bottom constraints, if I set it then my textview stretches in height when it loads and then it breaks textview height adjustment during typing. And I can't find any working solution without setting bottom constraints.
Can anyone give me some pointers on how to fix this issue?

Add fixed text view height and set bottom constraints to greater than or equal to.

Related

Why doesnt my textview show up when I add autolayout constraints?

I have a view which contains a textview (which displays a question) at the top. And 4 labels (for various answer options) with a stack of option buttons at the foot.
When leave the 'question' textview without constraints it appears fine in portrait. When I rotate the handset the text doesnt expand across the width of the screen.
I thought the way to resolve this was to click the add constraints pin/align button and click the T bars adding '0' constraints for top trailing and leading.
However when I do that the textview doesn't display at all. Any idea why?
The height constraint is missing. When you don't provide any constraints to your UITextView Xcode automatically will add the constraint's but as soon as you try to pin the top, leading and trailing anchors Xcode will not add any constraint's automatically.
You have to pin the bottom anchor of the UITextView to any of your buttons or give it a height constraint will solve your problem. Because UITextView will not calculate it's height based on it's inner content size like UILabel or UITextField.
If you want to only display some text and no edit functionality then you can use a UILabel and set the number of lines to 0.

Scrollview doesn't scroll with multiline label in it

I have a multiline label inside a scrollview. I set up the content size, let's say to scrollView.contentSize.height = 2000
But the view doesn't scroll. There is barely any code in the project. What is going wrong?
The only thing is that I don't have constrain for the height of the label, because it will vary depending on the length of text.
It doesn't matter about the height. But what does matter is that you need to pin it to the bottom of the scroll view also.
By pinning it on the top and bottom it will use the label to set the content size and so allow it to scroll.
I suggest to add a UITableView instead of UIScrollView, adding one UITableViewcell that contains a UILabel. By setting the appropriate values of:
Label's constraints.
tableView's rowHeight.
tableView's estimatedRowHeight.
It should works fine for your case.
For more Information about setting a dynamic cell height, you might want to check this answer.
Hope that helped.

Autolayout - Make superview height equal to subview height + constant where subview is textView

I have a UIView and inside the UIView I have a UITextView. The UITextView does not scroll, instead I set it programmatically to its full height with the following line of code :
self.textView.sizeToFit()
Through the interface builder, I set the following constraints for the UITextView , including superView.bottom = textView.bottom + 25 :
But then this is the result that I get when I run the app :
If anybody has any idea how could I fix this to fit the whole 'extended' textView, that would be really appreciated if you could let me know.
Thanks in advance.
Use a label instead of a text view. The important difference is that a label has an intrinsic content size where as a text view doesn't (because it's intended to scroll its content). This allows the label to work with the constraint system to display all of the text (without you needing to calculate the size).
If you stay using the text view you should add a height constraint and calculate the required height and configure the constraint appropriately.

Move element when UITextView expands

I am trying to make a TextView in my scrollview look like the "Hoshi" TextField in this github repo raulriera TextFieldEffects
If I only have a textview with top/left/right constraint it will expand when I enter more text which is right.
But when I add a UIView under it, it will cut off the text if I enter alot of characters, what I want to do is to move the view/line under it along with the textview when it expands.
My constrains for the UIView is top space to textview >= 10 I thought that would move the View when the textview expands but nothing happens, instead the text gets cut off and the view wont move
Here is a image for the constraint:
So how can I make the view move once the textview expands?
Programmatically change the height constraint of the textview based on the text entered.
Remove the bottom constraints of the lable it will work as desired.That bottom constraints don't allow your lable to move down as your textView height increases.
I found a solution.
I had to set the UIView's bottom constraint to be >= low priority (250). That would make it expand together with the textview

Scrolling in TextView

I have a UITextView with Scrolling Enabled.
My problem is that the TextView is small, width for two lines, adding successive lines the scroll does not appear to move around the TextView.
In short, I need to know how to implement the "scroll" according to the TextView grows.
Thank you!
If there are more content in text view with respect to size of text view then by
yourTextView.scrollEnabled = YES;
will scroll add the scrolling behaviour in your text view.
But if your text view's content is equal or less than to your text view's size then there is no need to scroll the text.
By default the TextView scrolls in vertical direction. So, when your content inside TextView will become more than its frame.height, it will automatically allow you to scroll vertically.

Resources