Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!