ios - 在 UIContainerView swift ios 中调整 subview Controller 的大小

标签 ios swift autolayout

我正在创建一个应用程序,我必须在其中打开一个 View Controller 内的 2-3 个 View Controller ,因为我想为这些 View Controller 共享相同的滑动抽屉和导航栏。我关注了this教程。

我在 MainController 中使用了 ContainerView 并正确添加了子 Controller ,但我很难调整子 Controller 的大小以匹配 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 不知何故不起作用 subview Controller 未正确调整大小,因为在主 Controller 中,导航栏约为 64,并且相同高度的部分在 subview Controller 中从底部被剪裁。

MainController的元素约束

  1. 导航栏 left=top=right = 0, height = 64

  2. UIContainerView left=right=bottom=0 and top to navbar = 0

最佳答案

您可以简单地设置vc 的框架,而不是将约束应用于vc 的 view,即

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

关于ios - 在 UIContainerView swift ios 中调整 subview Controller 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45627451/

相关文章:

ios - 使用 NSURLSession 处理顺序下载

ios - 使用相同的默认 UIImage 对象进行单元重用/初始化

ios - 使用 UIButton Action 在 UITableViewCell 中呈现一个弹出窗口

ios - UITextField inputView - IBActions 未触发

ios - 使用 Swift 动画 View 高度

ios - 自动布局方面/乘数属性如何工作?

ios - [UITableView dequeueReusableCellWithIdentifier :forIndexPath:] 中的断言失败

iphone - 可以传递给 ios 中的着色器的统一数组的最大大小是多少?

ios - 使用属性在代码中创建 UI

ios - 自动布局违反平移旋转动画