Which Constraint we need to make dynamic View heig

2019-06-09 10:54发布

问题:

As I am new to iOS. So forgive me if it is duplicate or very basic question.

I am taking one View. Approx below is the size .

x :    5              y :  5
Width  : 590  Height  : 100

and I set constraint it

Top to superView  5
Trailing to superView 5
Leading to superView 5

Now I have one Label which have dynamic Text and the Text is too large.

And the Label Constraint is below

Top to superView  5
Trailing to superView 5
Leading to superView 5

and when i set the background color of the View the color is not set. If the Text is to Long. So how to set the Height of the View and also set background so that it looks clear.

Code :

public override void ViewDidLoad()
        {
            base.ViewDidLoad();


            lbl_one.Text = "This is a long label which have long text inside the writing. This is a long label which have long text inside the writing. This is a long label which have long text inside the writing. This is a long label which have long text inside the writing";
            lbl_one.LineBreakMode = UILineBreakMode.WordWrap;
            lbl_one.Lines = 0;


            view_main.BackgroundColor = UIColor.Red;
        }

If I give fix Height then it look like this .

Output :

回答1:

1. Give the below constraints to your view, height is according to your need. here I'm giving 80.

2. Change the height relationship.

3. Add aUILabel in your above UIview, and give below constraints.

--> leading, top, bottom, trailing to uiview and height i.e. 80.

4. set height relationship as you do with UIView.

5. Change the property of UILabel , Lines to zero

6. Now enjoy with your constraints.



回答2:

EDIT: Add bottom constraint to your view instead of height constraint.

I don't see and bottom constraint added to UIView, so the view height will be 0.

If you have added the height constraint to UIView, there is a probability that UILablel might be overlapping the UIView, so you are not able to see the background color.

Set the UIView height constraint this will solve your problem

You can also add height or bottom constraint to your UIView.