在UIContainerView中调整子视图控制器的大小(Swift iOS)

7
我正在创建一个应用程序,其中我必须在一个视图控制器中打开2-3个视图控制器,因为我希望为这些视图控制器共享相同的侧滑抽屉和导航栏。我已经按照教程进行了操作。
我在MainController中使用了ContainerView,并且子控制器已经正确添加,但我很难调整子控制器的大小以匹配容器视图。
 self.mainContainer.addSubview(vc.view)
 self.mainContainer.translatesAutoresizingMaskIntoConstraints = false
 addChildViewController(vc)
 NSLayoutConstraint.activate([
 vc.view.leadingAnchor.constraint(equalTo:mainContainer.leadingAnchor),
 vc.view.trailingAnchor.constraint(equalTo: mainContainer.trailingAnchor),
 vc.view.topAnchor.constraint(equalTo: mainContainer.topAnchor),
 vc.view.bottomAnchor.constraint(equalTo: mainContainer.bottomAnchor)
 ])
vc.didMove(toParentViewController: self)

我遇到了以下错误:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x170095c70 h=-&- v=-&- UIView:0x127e17630.midY == UIView:0x127e15070.midY + 32   (active)>",
    "<NSLayoutConstraint:0x1740970c0 V:|-(0)-[UIView:0x127e17630]   (active, names: '|':UIView:0x127e15070 )>",
    "<NSLayoutConstraint:0x174097020 UIView:0x127e17630.bottom == UIView:0x127e15070.bottom   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x174097020 UIView:0x127e17630.bottom == UIView:0x127e15070.bottom   (active)>

我认为"self.mainContainer.translatesAutoresizingMaskIntoConstraints = false" 这一行代码并没有生效,子视图控制器的大小没有被正确地调整。在主控制器中,导航栏大约有64个像素高,但是在子视图控制器中,同样高度的部分从底部被裁剪了。
以下是 MainController 元素的约束条件:
1. 导航栏的左边、顶部和右边设置为0,高度为64。 2. UIContainerView 的左、右、下均设置为0,顶部与导航栏对齐。

主容器限制 - iOS Geek
抱歉,我的意思是它属于MainController。我已经将元素放置在主控制器内。 - Sahil Manchanda
我应该发布一个演示代码作为参考,以编程方式添加约束条件吗?你可以看一下。 - iOS Geek
可能我做错了什么,试试Buddy吧。 - Sahil Manchanda
我可以拿到你的代码文件或Xcode项目吗? - iOS Geek
你可以从教程链接下载基础项目,我会发布代码文件。 - Sahil Manchanda
2个回答

10

不必对vcview应用约束,只需简单地设置vc的框架即可。

    vc.view.frame = self.containerView.bounds //Here
    self.containerView.addSubview(vc.view)
    self.addChildViewController(vc)
    vc.didMove(toParentViewController: self)

8

vc.view.translatesAutoresizingMaskIntoConstraints = false

这行代码表示视图控制器中的视图不使用自动布局。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接