Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

314
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda