我搞定了!我删除了蓝色容器视图并将其替换为滚动视图。
以下是代码,供其他有兴趣的人参考。我仍在努力使容器视图的高度根据内容动态变化。
这个教程对我帮助很大:教程
主视图控制器:
var container: ContainerViewController!
override func viewDidLoad() {
container!.segueIdentifierReceivedFromParent("first")
}
@IBAction func firstBtnPressed(_ sender: Any) {
let vc = "first"
container!.segueIdentifierReceivedFromParent(vc)
}
@IBAction func secondBtnPressed(_ sender: Any) {
container!.segueIdentifierReceivedFromParent("second")
}
@IBAction func thirdBtnPressed(_ sender: Any) {
container!.segueIdentifierReceivedFromParent("third")
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "container"{
container = segue.destination as! ContainerViewController
}
}
容器视图(紫色视图)
open class ContainerViewController: UIViewController {
//Manipulating container views
fileprivate weak var viewController : UIViewController!
//Keeping track of containerViews
fileprivate var containerViewObjects = Dictionary<String,UIViewController>()
/** Specifies which ever container view is on the front */
open var currentViewController : UIViewController{
get {
return self.viewController
}
}
fileprivate var segueIdentifier : String!
/*Identifier For First Container SubView*/
@IBInspectable internal var firstLinkedSubView : String!
override open func viewDidLoad() {
super.viewDidLoad()
}
open override func viewDidAppear(_ animated: Bool) {
if let identifier = firstLinkedSubView{
segueIdentifierReceivedFromParent(identifier)
}
}
override open func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func segueIdentifierReceivedFromParent(_ identifier: String) {
self.segueIdentifier = identifier
self.performSegue(withIdentifier: self.segueIdentifier, sender: nil)
}
override open func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == segueIdentifier {
//Remove Container View
if viewController != nil {
viewController.view.removeFromSuperview()
viewController = nil
}
//Add to dictionary if isn't already there
if ((self.containerViewObjects[self.segueIdentifier] == nil)) {
viewController = segue.destination
self.containerViewObjects[self.segueIdentifier] = viewController
} else {
for (key, value) in self.containerViewObjects {
if key == self.segueIdentifier {
viewController = value
}
}
}
self.addChildViewController(viewController)
viewController.view.frame = CGRect(x: 0,y: 0, width: self.view.frame.width,height: self.view.frame.height)
self.view.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
}
}
}