gpt4 book ai didi

ios - CGRectOffset 后 AutoLayout 不工作

转载 作者:行者123 更新时间:2023-11-29 02:26:44 26 4
gpt4 key购买 nike

我有一个名为 containerView 的 UIView。

我将其作为 subview 添加到 Controller View 的 Root View 中。我以编程方式向它添加了一些约束(我将其居中并使宽度与 super View 的宽度相差几个点)。

我已将一些 UILabel 添加到 containerView 作为 subview 。 UILabel 的高度决定了 containerView 的高度。

当用户点击屏幕时,containerView 会从 CGRectOffset() 向上移动,动画完成后,它会移回到原始位置。

CGPoint absolutePoint = self.containterView.frame.origin;
self.containerYConstraint.constant = -absolutePoint;
[UIView
animateWithDuration:0.5
animations:^
{
[viewForUpdate setNeedsUpdateConstraints];
}
completion:^(BOOL finished)
{
self.containerYConstraint.constant = 0;
[viewForUpdate setNeedsUpdateConstraints];
[self.containerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; // Remove all subviews
}];

我需要删除作为 subview 放入其中的 UILabel 并用不同的标签替换它们。然而,当我删除其中一个 UILabel 时,整个 containerView 就会丢失(我将背景设置为红色,这样我就可以看到它)。我删除了示例中的所有 subview ,但是当我尝试仅删除一个 subview 时,会发生相同的效果。

为什么会出现这种情况?这和自动布局有关系吗?另外,如果我想在删除其中一个 UILabels 后将其重新设置为中心,我该如何重新进行约束?

最佳答案

在使用 AutoLayout 时修改框架是一个禁忌。一旦您开始使用 AutoLayout,您实际上就是在告诉系统您希望它为您设置框架。

不是直接为框架设置动画,而是创建指向您的约束的属性并为这些约束设置动画。

关于ios - CGRectOffset 后 AutoLayout 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27432564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com