我在运行时动态地向superview添加子视图,并希望调整superview的大小以匹配该子视图。调整大小是很好的,但是当我添加子视图时,它不是居中的。第二个视图似乎是在第一个视图结束时添加的。
这是一段被多次调用并被传递给不同subViews的代码:
self.currentConstraints = [
NSLayoutConstraint(item: superView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: subView.frame.size.height),
NSLayoutConstraint(item: superView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: subView.frame.size.width)
]
superView.addConstraints(self.currentConstraints!)
addChildViewController(viewController)
superView.addSubview(viewController.view)我可以让subView在setFrameOrigin中来回移动,但这是根据它们在开始时错误定位的位置而上下移动的。
发布于 2016-06-16 22:38:57
最后,我放弃了使用约束,并在超级视图上使用setFrameSize。我之前跳过的这个方法是因为它所在的popover没有调整大小,所以我只是将它设置为一个委托,并在添加新的子视图时调整它的大小。
superView.setFrameSize(subView.frame.size)
self.delegate.subViewChanged(subView.frame.size)
addChildViewController(viewController)
superView.addSubview(viewController.view)https://stackoverflow.com/questions/37623991
复制相似问题