ios - UILabel 上的 constraint.constant 更改后,父 UIView 未调整大小

标签 ios uiview xamarin.ios autolayout

更新

现在已解决 问题是,当我更新 bottomConstraint 时,我将 Constant 设置为 bottom padding 属性。听起来很合理,但当然 Constant 应该设置为 0 - BottomPadding。这解释了为什么文本底部不可见,它被限制在其剪裁容器之外。

我有一个名为 PaddedLabel 的简单 UIView 自定义控件,它包装(不是继承)一个 UILabel

View 层次是

PaddedLabel -> UILabel

当 UILabel 上的约束更新其常量时,外部 View 不会更改高度。就好像外面的 UIView 看到的只是 Label 的 Height 作为它需要的 Height 而不是 Label 的 Height 加上常量。这是它的样子

enter image description here

在 UpdateConstraints 中,我添加了一些约束,如果有文本值,我将 Constraint 上的 Constant 设置为我想要填充的值,否则我将 Constant 设置为 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;
    }
}

设置 Text 属性后,我在内部 UILabel 上设置 Text 属性并调用 SetNeedsUpdateConstraints

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

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

        this.text = value;
        this.nestedLabel.Text = value;
        this.SetNeedsUpdateConstraints();
    }
}

最佳答案

如果您希望 PaddedLabel View 展开并适合内部 UILabel,请更改底部约束。您想要将 PaddedLabel 的底部绑定(bind)到 UILabel 的底部,因此随着 UILabel 的增长,它会使 PaddedLabel 扩展!现在的方式是告诉 UILabel 在 PaddedLabel View 中挤压自己。

反转 bottomConstraint,你应该被设置。

关于ios - UILabel 上的 constraint.constant 更改后,父 UIView 未调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32987585/

相关文章:

ios - 来自 URL 的图像的阿拉伯文标题未显示在 UIImageView 中

iphone - UINavigationBar 的图像大小是多少

ios - UIView 渐变颜色在 UICollectionView 中未正确渲染

.net - 设计可移植到 iOS/Monotouch 的 Windows Phone 游戏的技巧

c# - 单点触控 : send data back down the stack to another ViewController

ios - iOS设备上的Firebase InstanceID token 刷新延迟

ios - 数组中的最后一个元素为 nil

ios - 基于 subview 自动设置容器 View 的高度

iphone - UIView block 动画转换与显示/隐藏键盘上的动画内容

iPhone 应用程序 : Linking to review page