Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

321
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda