我想将PlansViewController2添加到OverviewController的ContainerView中。我真的不知道如何将子视图添加到我的ContainerView中,以便可以向前和向后导航。我希望有人能帮助我!
_ = collectionView.rx.modelSelected(String.self).subscribe(onNext: { pnc in
let storyBoard = UIStoryboard(name:"Main", bundle:nil)
let action = self.actions.first(where: { $0.label == pnc })
let newController = storyBoard.instantiateViewController(withIdentifier: action!.control)
newController.navigationItem.title = pnc
self.containerView.addSubview(newController.view)
})
您可以看到here,第二个ViewController推送了一个新的Controller,但我希望它像上面的图片一样显示在容器View中。
答案 0 :(得分:0)
ContainerViewController.swift
import UIKit
class ContainerViewController: UIViewController {
@IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func btnOne(_ sender: Any) {
containerView.subviews.forEach { $0.removeFromSuperview() } // removes the old if have
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "NavigationOneViewControllerID") as! NavigationOneViewController
addChildView(viewController: nextViewController, in: containerView)
}
@IBAction func btnTwo(_ sender: Any) {
containerView.subviews.forEach { $0.removeFromSuperview() } // removes the old if have
}
@IBAction func btnThree(_ sender: Any) {
containerView.subviews.forEach { $0.removeFromSuperview() } // removes the old if have
}
private func addChildView(viewController: UIViewController, in DisView: UIView) {
viewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
viewController.view.frame = DisView.bounds
addChild(viewController)
DisView.addSubview(viewController.view)
viewController.didMove(toParent: self)
}
}
NavigationOneViewController.swift
import UIKit
class NavigationOneViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
ContainerSubOneViewController.swift
import UIKit
class ContainerSubOneViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func nxtClk(_ sender: Any) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "ContainerSubTwoViewControllerID") as! ContainerSubTwoViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
}
}
ContainerSubTwoViewController.swift
import UIKit
class ContainerSubTwoViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
在情节提要中将navigationOneViewController顶部栏设置为none。