I've tried following numerous examples and other questions on SO. The view controller is being added but it is placing the view container into the view and not the view container. Here is a shot of what I mean.
I have a tab bar on the primary view controller and a container view. When an item on the tab bar is tapped, it is supposed to grab the controller and load it into the container view. Here is the code for how I am doing it.
@IBOutlet weak var tabBar: UITabBar!
@IBOutlet weak var containerView: UIView!
var currentViewController: UIViewController!
var radarViewController: UIViewController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let storyboard = self.storyboard
let currentViewController = storyboard?.instantiateViewController(withIdentifier: "WeatherCurrentViewController") as! WeatherCurrentViewController
self.currentViewController = UINavigationController(rootViewController: currentViewController)
let radarViewController = storyboard?.instantiateViewController(withIdentifier: "WeatherRadarViewController") as! WeatherRadarViewController
self.radarViewController = UINavigationController(rootViewController: radarViewController)
}
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 1 {
containerView.addSubview(currentViewController!.view)
currentViewController.didMove(toParentViewController: self)
}
if item.tag == 2 {
containerView.addSubview(radarViewController!.view)
radarViewController.didMove(toParentViewController: self)
}
}
This is Swift 3. I don't know what it is I am doing incorrectly. Any help is appreciated.
Update
Although I never got the switching of view to work programmatically after numerous changes/tries, I did find another way of using segues with multiple view containers (here. Not the ideal since it ties up more memory but it works.