0

I'm trying to load all controllers in the main controller. Trying to use main controller as a navigation controller.

I try modal segue but I can only load the first one because even if i can see the navigation on the main controller I'm not able to press it.

I think that the top controller have to be resized to contrains but I have not being able to find how.

Here is the screenshot of what I'm trying to achieve.
The controllers need to go in the orange section.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
R.Bruguera
  • 13
  • 4
  • To understand your question more clearly, why are you not embedding the main View Controller in a navigaton controller? That will make loading other view controllers easy by just pushing them onto the navigation stack. – as diu Sep 04 '17 at 17:41
  • This might be of help to you : https://stackoverflow.com/a/11141688/8071224 – as diu Sep 04 '17 at 23:13

1 Answers1

0

Here is the answer. Found an old tutorial and just made some adjustments. Maybe helps someone else.

Picture

MenuCV

import UIKit

class MenuCV: UIViewController {
@IBOutlet weak var tableView: UITableView!
var containerView: ContainerVC?
var menuItems = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    menuItems = ["Dashboard", "Companies", "Makes"]

}

}

extension MenuCV: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return menuItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    guard let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell") as? MenuTVC else { return UITableViewCell() }

    let image = UIImage(named: "logo")

    cell.configureCell(menuIcon: image!, menuLbl: menuItems[indexPath.row])

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    containerView?.segueIdentifierReceivedFromParent(button: menuItems[indexPath.row])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "container" {
        containerView = segue.destination as? ContainerVC
    }
}

 }

ContainerVC

import UIKit

class ContainerVC: UIViewController {

var vc : UIViewController!
var segueIdentifier : String!
var lastViewController: UIViewController!

override func viewDidLoad() {
    super.viewDidLoad()
    segueIdentifierReceivedFromParent(button: "Dashboard")
}

func segueIdentifierReceivedFromParent(button: String){

self.segueIdentifier = button
    self.performSegue(withIdentifier: self.segueIdentifier, sender: nil)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == segueIdentifier{
        //Avoids creation of a stack of view controllers
        if lastViewController != nil{
            lastViewController.view.removeFromSuperview()
        }
        //Adds View Controller to Container
        vc = segue.destination
        self.addChildViewController(vc)
        vc.view.frame = CGRect(x: 0,y: 0, width: self.view.frame.width,height: self.view.frame.height)
        self.view.addSubview(vc.view)
        vc.didMove(toParentViewController: self)
        lastViewController = vc

    }
}

}

DashboardVC

import UIKit

class DashboardVC: UIViewController {


override func viewDidLoad() {
    super.viewDidLoad()

}

}

CompaniesVC

import UIKit

class CompaniesVC: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

}

}

MakesVC

import UIKit

class MakesVC: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

}

}

EmptyVC

import UIKit

class EmptyCV: UIStoryboardSegue {

override func perform() {

}

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
R.Bruguera
  • 13
  • 4