Add child view controller to UINavigationController

后端 未结 3 1270
南笙
南笙 2021-01-31 05:30

I\'m trying to add a child view controller to a UIViewController contained in a UINavigationController with this code:

- (void)buttonTa         


        
3条回答
  •  感动是毒
    2021-01-31 05:54

    @Pwner's answer Swift version:

    Add child to UINavigaitonController

    let child = MyChildViewController()
    self.navigationController?.addChildViewController(child)
    guard let navigationController = navigationController else {
        return
    }
    child.view.frame = navigationController.view.bounds
    child.beginAppearanceTransition(true, animated: true)
    self.navigationController?.view.addSubview(child.view)
    self.view.alpha = 0
    UIView.animate(withDuration: 0.3, animations: {
        child.view.alpha = 1.0
    }, completion: { _ in
        guard let navigationController = self.navigationController else {
            return
        }
        child.endAppearanceTransition()
        child.didMove(toParentViewController: navigationController)
    })
    

    Remove a child from UINavigationController

    child.willMove(toParentViewController: nil)
    child.beginAppearanceTransition(false, animated: true)
    UIView.animate(withDuration: 0.3, animations: {
        child.view.alpha = 0.0
    }, completion: { _ in
        guard let navigationController = self.navigationController else {
            return
        }
        child.view.removeFromSuperview()
        child.endAppearanceTransition()
        child.removeFromParentViewController()
    })
    

提交回复
热议问题