【发布时间】:2020-10-10 05:41:31
【问题描述】:
我看到了几个与此问题相关的问题,但没有适用于我的案例的答案。
我在容器 (sampleContainer) 内有一个视图控制器 (sampleVC),无论您从哪个位置开始,它都能完美加载,但在从横向更改为纵向或反之亦然后,它不会调整大小。
在我的视图控制器中,我有:
func addSampleVC() {
self.addChild(self.sampleVC)
self.sampleVC.didMove(toParent: self)
self.sampleContainer.addSubview(self.sampleVC.view)
self.addSampleConstraints()
}
func addSampleConstraints() {
sampleContainer.bounds = CGRect(x: 0, y: -20, width: sampleContainer.bounds.width, height: sampleContainer.bounds.height)
sampleVC.view.bounds = CGRect(x: 0, y: 0, width: sampleContainer.bounds.width * 0.94, height: sampleContainer.bounds.height)
sampleVC.view.leftAnchor.constraint(equalTo: sampleContainer.leftAnchor, constant: 20).isActive = true
sampleContainer.clipsToBounds = true
NSLayoutConstraint.constrain(view: sampleVC.view, containerView: sampleContainer)
}
函数 addSampleVC 从 viewDidLoad 调用。根据其他答案,我尝试在 viewWillTransition 中获取更新的容器边界,然后调用 setNeedsLayout 和 setNeedsUpdate,但我一定遗漏了一些东西,因为似乎没有任何效果。感谢您的任何想法。
【问题讨论】:
标签: ios swift ipad orientation