【问题标题】:Resizing Child View Controllers in UIContainerView swift ios在 UIContainerView swift ios 中调整子视图控制器的大小
【发布时间】:2018-01-19 12:20:08
【问题描述】:

我正在创建一个应用程序,我必须在视图控制器中打开 2-3 个视图控制器,因为我想为这些视图控制器共享相同的滑动抽屉和导航栏。我已经关注this 教程。

我在 MainController 中使用了 ContainerView 并且正确添加了子控制器,但我在调整子控制器的大小以匹配 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 无法正常工作 子视图控制器没有正确调整大小,因为在主控制器中 navbar 大约为 64 并且相同高度的部分从底部被剪裁在子视图控制器中。

MainController的元素约束

  1. 导航栏左=上=右=0,高度=64

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

【问题讨论】:

  • 主要容器约束?
  • 对不起,我的意思是它的 MainController。我放置在主控制器中的元素
  • 我应该发布一个演示代码以供参考以编程方式添加约束吗?你可以去看看
  • 试试哥们可能是我做错了什么
  • 我可以得到你的代码文件吗?还是 Xcode 项目?

标签: ios swift autolayout


【解决方案1】:

不用对vc'sview施加约束,你可以简单地设置vc的框架,即

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

【讨论】:

    【解决方案2】:

    vc.view.translatesAutoresizingMaskIntoConstraints = false

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-29
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多