triff
triff

Reputation: 157

ContainerView - how close invisible view

I have this storyboard:

enter image description here

and this code:

var actualVisibleView : String? = nil

func showSubViewInContainerView(view: String){
        let controller = storyboard!.instantiateViewController(withIdentifier: view)
        addChildViewController(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false

        systemContainerView.addSubview(controller.view)

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

        controller.didMove(toParentViewController: self)

        if self.actualVisibleView != nil && self.actualVisibleView != view {
            controller.dismiss(animated: false) {
                print("UBIJAM: \(view)")
            }
        }
        self.actualVisibleView = view
        print("OTWIERAM: \(view)")
    }

From the left menu, I open various views in this containerview using the code:

showSubViewInContainerView(view: "view1")
showSubViewInContainerView(view: "view2")
showSubViewInContainerView(view: "view3")
showSubViewInContainerView(view: "view4")

This code works light. The only problem is that when I open a new view in the container view I would like to close the previously visible view. Only one active view will be visible in containerview.

At the moment there are views overlapping one another.

Does anyone know how to fix it?

Upvotes: 0

Views: 59

Answers (1)

RajeshKumar R
RajeshKumar R

Reputation: 15758

Before adding a new view in the container remove other views

systemContainerView.subviews.forEach { $0.removeFromSuperview() }
systemContainerView.addSubview(controller.view)

Upvotes: 2

Related Questions