简体   繁体   English

更改超级视图框架后更新子视图的约束

[英]Update constraints of subviews after superview frame changes

I have a UIView subclass (to deal with drag and drop) and with Interface Build I added two subviews: an UIImageView and an UILabel . 我有一个UIView子类(用于处理拖放),并使用Interface Build添加了两个子视图: UIImageViewUILabel

Both have constraints: 两者都有约束:

UIImageView border match the superview borders in order to fill all the available space. UIImageView边框与UIImageView边框匹配,以填充所有可用空间。 UILabel is centered vertical and horizontally. UILabel垂直和水平居中。

Here you can see the three (superview background is green). 在这里您可以看到三个(超级背景为绿色)。

在此处输入图片说明

My problem comes when I animate a frame change in the superview. 当我为超级视图中的帧变化设置动画时,我的问题就来了。 The superview seems fine, but its subviews go crazy. 超级视图看起来不错,但其子视图变得疯狂。

Here you can see it (ignore the brown square, is from another view) 在这里您可以看到它(忽略棕色正方形,是从另一个角度来看的)

在此处输入图片说明

As you can see, subviews "fall" from the superview, despite I call updateConstraints on every subview. 如您所见,尽管我在每个子视图上都调用updateConstraints ,但子视图还是从父视图中“掉下”了。

I know it is a bad practice to mix AutoLayout with setFrame, but is the only way I know to do it. 我知道将AutoLayout与setFrame混合使用是不好的做法,但这是我知道的唯一方法。

Thanks 谢谢

Thanks to the comments I made some changes. 感谢评论,我做了一些更改。

1) Change the superview (the green one) with the UIImageView. 1)使用UIImageView更改超级视图(绿色的)。

2) Remove the UILabel and use a CATextLayer to place the text, using this answer to center the text vertically in front of the UIImageView. 2)删除UILabel并使用CATextLayer放置文本,使用此答案将文本垂直居中放置在UIImageView的前面。

And now it works fine. 现在工作正常。 Thanks for the help. 谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM