3

TLDR: I would like to ensure that my remove code removes the Container View and UIViewController A, B, and C. Any help is greatly appreciated.

My application has a container view which adds a container view programmatically. The view in the container view has a Navigation Controller and pushes to various outer UIViewControllers. For example, View A is in the Container View (with a Navigation Controller), and this pushes to B, which pushes to C. There is an option on UIViewController A, B & C to remove the Container View. However, when I trigger this option to dismiss the Container View in any other UIViewController than A then the View does not get dismissed (still visible on screen). e.g If I trigger it on UIViewController B, UIViewController Bs view is still present.

Here is how I create my container view:

// add container

containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
NSLayoutConstraint.activate([
    containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
    containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
    containerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
    containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
    ])

// add child view controller view to container

controller = storyboard!.instantiateViewController(withIdentifier: "A")
addChildViewController(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(controller.view)

NSLayoutConstraint.activate([
    controller.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
    controller.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
    controller.view.topAnchor.constraint(equalTo: containerView.topAnchor),
    controller.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
    ])

controller.didMove(toParentViewController: self)

taken from - https://stackoverflow.com/a/40102513

And my remove code is:

controller.willMove(toParentViewController: nil)
controller.view.removeFromSuperview() //Im believe here may be the problem
controller.removeFromParentViewController()
containerView.isHidden = true

UIViewController As view is added containerView.addSubview(controller.view) so I am guessing this is why the remove code will not work on UIViewController B or C. I would like to ensure that my remove code removes the Container View and UIViewController A, B, and C. Any help is greatly appreciated?

Community
  • 1
  • 1
DevC
  • 6,982
  • 9
  • 45
  • 80
  • Is `A` a `UINavigationController` laid out in your storyboard, where `B` is its Root View Controller? It's not clear, as you say you have an *"option on UIViewController A, B & C to remove the Container View"* ... but that wouldn't make sense if A was the navigation controller. This makes me wonder just how you are putting "B" on screen - and may be the answer to why it's not being removed as you expect it to be. – DonMag May 14 '17 at 22:05

0 Answers0