UIView addSubview自动布局无法正常工作

yon*_* ho 4 uiview ios autolayout

我以编程方式创建了一个UIView并将NSLayoutConstraint添加到它并且它可以工作,然后将其添加为视图控制器视图的子视图.现在我需要从superview中删除此视图,稍后再添加一次.但是如果我再次添加它,布局将不再起作用self.view addSubview.如何使布局再次工作?

小智 16

问题是子视图不会继承原始视图的大小/框架,并且用于添加视图,就好像它被卡在Any + Any尺寸的600x600中一样.我的解决方案是设置原始视图的框架,然后让iOS完成其余的工作.

[self.myOtherView setFrame:self.view.frame];   //Replace frame size with already resized view
[self.myOtherView setNeedsLayout];  //Tell iOS to autolayout the new sub view
[self.addSubView:self.myOtherView];  //Add it
Run Code Online (Sandbox Code Playgroud)