Parent UIView does not resize after restriction. Constant on UILabel changed

UPDATE

Now allowed . The problem was that when I updated bottomConstraint, I set Constantpadding to the bottom property. Sounds reasonable, but of course the value Constantshould be set to 0 - BottomPadding. This explains why the bottom of the text was not visible, it was limited outside the cut-off container.

I have a simple UIView user element called PaddedLabel that wraps (does not inherit) a UILabel

Presentation hierarchy

PaddedLabel → UILabel

When UILabel constraints update their constants, appearance does not increase height. This is similar to the fact that the external UIView sees only the height of the label as the required height, and not the height of the label plus constants. Here is what it looks like

enter image description here

In UpdateConstraints, I add some restrictions, and if there is a text value, I set Constant in Constraint to the value I want to complement else, I set the constant to 0.

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

    if (this.constraintsApplied == false)
    {
        this.leftConstraint = 
            NSLayoutConstraint.Create(this.NestedLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1.0f, this.LeftPadding);
        this.AddConstraint(this.leftConstraint);

        this.rightConstraint =
            NSLayoutConstraint.Create(this.NestedLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1.0f, 0 - this.RightPadding);
        this.AddConstraint(this.rightConstraint);

        this.topConstraint =
            NSLayoutConstraint.Create(this.NestedLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1.0f, this.TopPadding);
        this.AddConstraint(this.topConstraint);

        this.bottomConstraint =
            NSLayoutConstraint.Create(this.NestedLabel, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this, NSLayoutAttribute.Bottom, 1.0f, 0 - this.BottomPadding);
        this.AddConstraint(this.bottomConstraint);

        this.constraintsApplied = true;
    }

    if (this.Text.HasValue())
    {
        this.topConstraint.Constant = this.TopPadding;
        // The following code was the problem. 
        // It should have been 0 - this.BottomPadding Now corrected
        // this.bottomConstraint.Constant = this.BottomPadding;</del>
        this.bottomConstraint.Constant = 0 - this.BottomPadding;
    }
    else
    {
        this.topConstraint.Constant = 0;
        this.bottomConstraint.Constant = 0;
    }
}

When the Text property is set, I set the Text property to an internal UILabel and call SetNeedsUpdateConstraints

public string Text
{
    get
    {
        return this.text;
    }

    set
    {
        if (this.text == value)
        {
            return;
        }

        this.text = value;
        this.nestedLabel.Text = value;
        this.SetNeedsUpdateConstraints();
    }
}
+4
source share
1 answer

, PaddedLabel UILabel, . PaddedLabel UILabel, UILabel , PaddedLabel! , UILabel, PaddedLabel.

.

0

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


All Articles