UIStoryboardSegue 嵌入

UIStoryboardSegue embed

在 Interface Builder 的 UIView(添加为容器视图)中嵌入 segue 到底有什么作用?我正在尝试以编程方式嵌入 ViewController,因为我想在两个不同的选项之间进行选择。我想我正确设置了容器视图的限制约束,因为当我在情节提要中添加带有嵌入 segue 的子视图控制器时,一切正常,但是一旦我将其与以下代码一起嵌入,视图控制器就会与底部约束重叠容器视图,它弄乱了我的布局..

let viewController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "MyViewController")
self.containerView.addSubview(viewController.view)
self.addChildViewController(viewController)
NSLayoutConstraint.activate([
    viewController.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
    viewController.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
    viewController.view.topAnchor.constraint(equalTo: containerView.topAnchor),
    viewController.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])

viewController.didMove(toParentViewController: self)

感谢@Dinesh Balasubramanian,我现在得到了以下有效的代码:

let viewController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "MyViewController")
self.containerView.addSubview(viewController.view)
self.addChildViewController(viewController)
viewController.view.frame = containerView.bounds
viewController.didMove(toParentViewController: self)