[英]iOS removing an object from superview demolishes auto layout and disables scrollview
我有一个按钮,该按钮以编程方式进行布局,并具有手动设置的约束条件。 然后,我尝试通过调用removeFromSuperview
从视图中删除按钮。 删除按钮会导致位于按钮下方的集合视图子视图丢失其在scrollview
视图中的位置, scrollview
自身与所有其他对象后面的超级superview
顶部对齐。 我希望集合视图可以简单地移动到按钮曾经是按钮的位置,并且其约束已从视图中删除。 我想念什么?
按钮创建:
aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton addTarget:self
action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:@"Expand" forState:UIControlStateNormal];
[aButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:aButton];
卸下按钮:
- (void)buttonPressed:(UIButton *)sender{
NSLog(@"Pressed");
[aButton removeFromSuperview];
}
垂直定位(detailOfLocation是集合视图):
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[locationImageContainer(213)][locationTitle][locationDescription][aButton][detailOfLocation]|"
options:0
metrics:0
views:viewArranging]];
[编辑]
删除按钮似乎也禁用了UIScrollView的滚动功能,UIScrollView是放置所有内容的超级视图。
删除按钮后,您可能会有一些不足的约束,因此也许您应该对此进行补偿。 例如,您可能想在locationDescription
和detailOfLocation
之间添加一个约束,因为移除按钮后它们的垂直位置变得不明确。 scrollview滚动也可能与约束有关。 看看这个答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.