我想把我的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)
})
如您所见,第二个View Controller推送一个新控制器,但我希望它显示在容器视图中,如上图所示。
对于代码块中显示的代码,我遇到了一些困难。我不知道怎么从那里继续下去。我在我的“actions”数组中实例化了我的视图,但是不能再进一步了。
最佳答案
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)
}
}
导航neviewcontroller.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.
}
}
在情节提要中将NavigationOneViewControl顶部栏设置为“无”。
关于ios - 如何将第二个View Controller也添加到ContainerView中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56389766/