Which Constraint we need to make dynamic View heig

2019-06-09 10:59发布

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 :

enter image description here

2条回答
Luminary・发光体
2楼-- · 2019-06-09 11:24

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

Constraints to view

2. Change the height relationship.

Change height relationship

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

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

enter image description here

4. set height relationship as you do with UIView.

5. Change the property of UILabel , Lines to zero

enter image description here

6. Now enjoy with your constraints.

查看更多
对你真心纯属浪费
3楼-- · 2019-06-09 11:41

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 problemEither add the bottom constraint or height as shown in image

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

The UIlabel should have the shown constraints with Background color as clear color

查看更多
登录 后发表回答