UIStoryboardSegue嵌入

时间:2018-05-14 15:55:14

标签: ios swift uiscrollview uicontainerview

在界面构建器中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)

1 个答案:

答案 0 :(得分:-1)

感谢@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)