Tim J
Tim J

Reputation: 1271

How can I check if a child view controller exists

I am implementing a slide in style menu.

The menu is added as a child view controller on show, then animated into view. I then remove it from the view once it has been closed.

I would like to introduce a UIPanGestureRecognizer so the user can swipe it into view, however the logic for adding the view is only triggered when pressing open.

I'd like to avoid adding it many times and on every gesture, so I was thinking of checking if it was present, if not add it, then animate.

lazy var menuController = MenuController()

private var menuWidth: CGFloat = 300
private let keyWindow = UIApplication.shared.keyWindow

override func viewDidLoad() {
    super.viewDidLoad()

    setupNavigationItems()
    setupTableView()

    menuController.view.frame = CGRect(x: -menuWidth, y: 0, width: menuWidth, height: view.frame.height)

    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
    view.addGestureRecognizer(panGesture)

}

@objc func handlePan(gesture: UIPanGestureRecognizer) {


    let translation = gesture.translation(in: view)
    let transform = CGAffineTransform(translationX: translation.x, y: 0)
    menuController.view.transform = transform
    navigationController?.view.transform = transform

}

@objc func handleOpen() {
    keyWindow?.addSubview(menuController.view)
    animateMenuController(transform: CGAffineTransform(translationX: self.menuWidth, y: 0)) { }

    addChild(menuController)

}

@objc func handleHide() {
    animateMenuController(transform: .identity) { [weak self] in
        self?.menuController.view.removeFromSuperview()
        self?.menuController.removeFromParent()
    }
}

I was hoping to do something like this

    @objc func handlePan(gesture: UIPanGestureRecognizer) {

        if view.subviews.contains(MenuController) {
            print("yes")
        }


        let translation = gesture.translation(in: view)
        let transform = CGAffineTransform(translationX: translation.x, y: 0)
        menuController.view.transform = transform
        navigationController?.view.transform = transform

    }

But this is not correct.

Upvotes: 3

Views: 5853

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100533

You can try to use childrens property of the vc

if !children.isEmpty { // this assumes 1 vc tell if you have more
   print("YES")  
}

or

if let _ = children.first(where:{ $0 is menuController})  {  // this assumes 1 vc tell if you have more
   print("YES")  
}

you may need also to add it to

 view.addSubview(menuController.view)

not to keyWindow

Upvotes: 6

nodediggity
nodediggity

Reputation: 2488

You could check the classForCoder against your class name

if children.first(where: { String(describing: $0.classForCoder) == "MenuController" }) != nil {
    print("we have one")
}

This does introduce a "magic string" however as simply changing your class name would break this logic.

Upvotes: 4

Related Questions