【发布时间】:2018-01-25 10:38:03
【问题描述】:
我的视图控制器有一个 NSView,我将其用作容器视图。 我从库中拖出“容器视图”,删除了 segue 和嵌入式视图控制器,为超级视图(视图控制器的主视图)添加了约束,并在视图控制器的代码中为容器视图设置了一个出口。
到目前为止一切顺利。
我希望将容器视图的内容与运行时确定的几个不同视图控制器的主视图交换。我已经成功做到了,代码如下:
func swapContainerViewContents(with viewController: NSViewController) {
self.containerView.subviews.foreach({ subview in
subview.removeFromSuperview()
})
containerView.addSubview(viewController.view)
}
这可行,但是一旦我调整窗口大小,子视图显然会保持其原始大小(我没有设置任何约束)。
如果相反,我添加必要的约束:
let newView = viewController.view
containerView.addSubview(newView)
newView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
newView.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
newView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
newView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
...现在,窗口无法调整大小! (它停留在子视图的初始大小)。
这样做的方法是什么?
【问题讨论】: