ios – 在UILabel上的constraint.constant发生更改后,父UIView没有调整大小

ios – 在UILabel上的constraint.constant发生更改后,父UIView没有调整大小,第1张

概述UPDATE 现在解决问题是当我更新bottomConstraint时,我将Constant设置为底部填充属性.听起来很合理但当然Constant应该设置为0 – BottomPadding.这就解释了为什么文本底部不可见,它被限制在剪切容器之外. 我有一个简单的UIView自定义控件,名为PaddedLabel,它包装(不继承)UILabel 视图层次结构是 PaddedLabel – >的UI UPDATE

现在解决问题是当我更新bottomConstraint时,我将Constant设置为底部填充属性.听起来很合理但当然Constant应该设置为0 – Bottompadding.这就解释了为什么文本底部不可见,它被限制在剪切容器之外.

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

视图层次结构是

PaddedLabel – >的UILabel

当UILabel上的约束更新其常量时,外部视图不会达到高度.就好像外部UIVIEw只看到标签的高度作为它需要的高度而不是标签的高度加上常量.这是它的外观

在UpdateConstraints中我添加了一些约束,如果有一个Text值,我将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,1.0f,this.leftpadding);        this.AddConstraint(this.leftConstraint);        this.rightConstraint =            NSLayoutConstraint.Create(this.nestedLabel,NSLayoutAttribute.Right,0 - this.Rightpadding);        this.AddConstraint(this.rightConstraint);        this.topConstraint =            NSLayoutConstraint.Create(this.nestedLabel,NSLayoutAttribute.top,this.toppadding);        this.AddConstraint(this.topConstraint);        this.bottomConstraint =            NSLayoutConstraint.Create(this.nestedLabel,NSLayoutAttribute.Bottom,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视图展开并适合UILabel内部,请更改底部约束.你想把PaddedLabel的底部绑在UILabel的底部,所以随着UILabel的增长,它会使PaddedLabel扩展!现在的样子,你告诉UILabel在PaddedLabel视图中挤压自己.

反转bottomConstraint,你应该设置.

总结

以上是内存溢出为你收集整理的ios – 在UILabel上的constraint.constant发生更改后,父UIView没有调整大小全部内容,希望文章能够帮你解决ios – 在UILabel上的constraint.constant发生更改后,父UIView没有调整大小所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1003770.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存