Move element when UITextView expands - ios

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

Related

Move textview of variable height when keyboard appears

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.

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.

Dynamic Scrollview Swift

I have made a Button which generates TextFields and I want a ScrollView so if I'd add too much TextFields I could scroll and write to them. I also have a Button at the bottom of my view and I want this to be visible all the time. (I don't mind if only TextFields will be inside the ScrollView or the whole view except my bottom button).
You have to set the height constraint on your TextFields. Without that, the scrollView can't figure out its contentSize. After you set the heights, the scrollView will adapt accordingly.
Also, I would use a UIView and give a fix height for it and place the TextFields inside of the UIView.
You can also use scrollView.contentSize = CGSize(width: expectedWidth, height: expectedHeight)
These steps help you to design screen according to your requirement :
Drag a ScrollView and provide height and width accordingly
Decrease height of ScrollView by height of Button which you always want to show on bottom of page
i.e- Button should be outside of ScrollView not within the ScrollView
Adjust button size and height and put it below to ScrollView
Now set Constraint to each one . First of all select ScrollView provide its l
Left , Right , Top constraint .
Select ScrollView and by pressing Control + drag mouse to Button ( below of Scrollview) and set vertical spacing between ScrollView and Button
Select Button and provide Left , Right , Bottom Constraint.
Note - As AutoLayout works differently with ScrollView so always set constraint of all control within it from Order : Top To Bottom
Hope it helps you to fix this issue . Feel free to comment if still any confusion.
Thanks.

UITextView autogrowing with new line inside UITableViewCell

I have a textview as shown in the picture below,
I have given auto-layout constraint as top and bottom to thin line and leading to label and trailing to superview. Now whenever the return key is pressed the bounds of the textview automatically changes disobeying the constraints and the views get garbled as shown in picture below,
I don't want this behaviour, Please help, thanks!.
You can use following example for growing textview.
https://github.com/MatejBalantic/MBAutoGrowingTextView
Give a height constraint to you UITextView

Add view under text view

I have a fullsceen textView including much strings, so height of this view automatically changes. Its works nice, but i need to create some newView under this textView.
If I just make constraints like bottom of screen = bottom of newView, top of newView = bottom of textView, so newView will overlap textView.
try add height constraints to your textView with priority is 750.
Now you can build your app and run it again!
Goodluck
Just add your text view to the new view.
Here you are adding view with same size of textview. So it will over lapping. But if you add your text view subview of view so your main view will go back and text view will come front.
I think the problem is your views are trying to fulfil their constraints on fixed screen size.
If you want to keep it on one page (no scrolling) you can either add height constraints to your text view such that its height won't go over certain value or set the bottom view's constraint priority higher so that even when the textview expand it won't overrule bottom view's constraint.
Otherwise you might want to add ScrollView underneath your views. This way when your textview expands the bottom view will get pushed to the bottom outside the screen and you can scroll down the page

Resources