How to change uiview height using auto layout?

So, I created this scenario to understand how views increase in height according to their content. However, I still cannot do this.

This is what I have now:

enter image description here

The text image grows in accordance with the content. however, the uiview it contains disappears. What restrictions should be used so that as uitextview grows in size, its parent view also grows in height?

+5
source share
3 answers

ON UITextView makes your selection unselected. These

  • Scrolling Enabled
  • Bounces
  • Horizontal Bounce
  • Rebound vertically
  • Shows a horizontal indicator
  • Shows a vertical indicator

Now change the auto layout settings this way.

enter image description here

Constraints for parent view

+1
source

Start by clearing all your limitations from everything so that we have a new slate.

Step 1: Create a hierarchy of your presentation. In this example, we need something like this:

enter image description here

In the view controller's view, which we will call parentView , there is a subview that we will call redView . That redView has a child, a text field that we will call textField .

The hierarchy is as follows:

enter image description here

Step 2: Define any restrictions specific to any individual species. In this case, we probably only want to set a width limit on our text view. So far, I just set the width to 200pts.

enter image description here

Step 3: Define the restrictions between the textView and its parent, redView . Let them say that we need a 10pt border. Add these restrictions:

enter image description here

As soon as we add these restrictions, we will receive warnings and errors in the automatic layout. Firstly, because the restrictions that I added for and to view the supervisor do not match the actual sizes, I will get a few warnings:

enter image description here

There will also be errors describing the lack of X and Y positions for redView and for textView . Here, if necessary, twice as many errors. textView knows where to place itself relative to redView . We do not need any more restrictions to sort the position of the textView . However, redView does not know where else to position ... and therefore, ultimately, textView also does not know exactly.

We can update the textView frame to get rid of the warnings, but release and correct the actual errors.

Step 5: set the redView restrictions relative to superView . redView already knows what size to be. Please note that we did not have errors for the width of redView . He just doesn't know where to be. In this case, I will go simply and say that we want redView be centered. Therefore, we want to add these restrictions:

enter image description here

Now we have fixed some problems. The only problem that remains is height for everything.

To fix this, we need to prioritize the size of the contents of the textView . Set them all to 1000 and change the "Internal Size" property to "Placeholder".

enter image description here

Currently, all auto-layout errors should disappear, and we should only be left with warnings because our storyboard framework does not match what our limitations say.

We can fix this by selecting parentView and updating all frames:

enter image description hereenter image description here

There is one final caveat to this auto layout puzzle when it comes to autosaving based on the size of the content: what happens if our text view does not contain content?

If there is no content in our text view, the auto market will select a height of 0, and our users will not even be able to see that there is a text view, but much less to add content (and expand it). When using automatic layout and content-based content determination, we almost always have to be sure that we set either explicit or minimum size to represent the content.

We don’t need to worry about our textView width, since we explicitly set it to 200. So add a minimum height constraint. Start by adding a height limit:

enter image description here

Now go to the size inspector for textView , find this height limit that we added, and change it to a limit greater than or equal to:

enter image description here

The storyboard will not reflect changes in the content in our textView and will textView accordingly, but your restrictions are now set correctly, and this will behave properly on your device or in the simulator.

+10
source

On the storyboard page, click on the text. Then click on the small triangle in the lower right.

Click "Clear Constraints" first. Then "Add Missing Restrictions."

This is the easiest way.

-2
source

Source: https://habr.com/ru/post/1207528/


All Articles