1

Using Xcode 10+, Swift 4, iOS 11.4+

First let me say that I'm not using a Navigation Controller - I'm adding a ViewController to another as a child using this basic code:

topController.addChildViewController(childVC)
topController.view.addSubview(childVC.view)
childVC.didMove(toParentViewController: topController)

The child is smaller than the parent and has a few buttons, one of which will animate it out of view.

I'm not using present/dismiss as it always covers the entire screen.

I'd like it to be modal - once it's animated into place, nothing else on screen (behind it) should be usable until it is animated out of view.

How can I make the childVC be modal?

wayneh
  • 4,393
  • 9
  • 35
  • 70

2 Answers2

0

You could try adding the controller to a UIWindow which has windowLevel = UIWindowLevelAlert + 1 instead. Then after the dismiss animation finishes you could remove the window. Here is a sample code snippet that seems to work:

func presentChildVC() {
    modalWindow = UIWindow(frame: UIScreen.main.bounds)
    let rootController = UIViewController()
    rootController.view.backgroundColor = .clear
    rootController.addChild(childController)
    rootController.view.addSubview(childController.view)
    childController.didMove(toParent: rootController)

    modalWindow?.rootViewController = rootController
    modalWindow?.windowLevel = .alert + 1
    modalWindow?.makeKeyAndVisible()
    modalWindow?.backgroundColor = .clear
    UIView.animate(withDuration: 2, animations: {
        self.childController.view.alpha = 1
    })
}

func dismissChildVC() {
    UIView.animate(withDuration: 2, animations: {
        self.childController.view.alpha = 0
    }, completion: { _ in
        self.modalWindow?.isHidden = true
        self.modalWindow = nil
    })
}
gulyashki
  • 449
  • 3
  • 5
  • I tried your suggestion but it just created a full-screen view which was non-responsive to my interaction. It is visually the same as using present/dismiss but doesn't respond to interaction. – wayneh Dec 28 '18 at 03:58
  • Did you set a frame to your child controller’s view? If so and it’s still covering the whole screen maybe you’ve set it as a rootController to the window and it shouldn’t be. I’m going to test the interactions but I added a gesture recognizer to the child VC view and it is working fine. – gulyashki Dec 28 '18 at 05:57
  • I ended up using this guide to create it and it works perfectly: https://medium.com/if-let-swift-programming/design-and-code-your-own-uialertview-ec3d8c000f0a – wayneh Dec 28 '18 at 19:07
0

1) The child is smaller than the parent:-

You just need to update your child view frame same like parent view.

topController.addChildViewController(childVC)
topController.view.addSubview(childVC.view)
**childVC.view.frame.size.height = self.view.frame.size.height**
childVC.didMove(toParentViewController: topController)

2) has a few buttons, one of which will animate it out of view :-

Set Click Event on buttons like this to remove child view from parent

self.willMove(toParentViewController: nil)
self.view.removeFromSuperview()
self.removeFromParentViewController() 
Rahul
  • 36
  • 1
  • Thanks for the suggestions, but my issues are not that I don't know how to remove the controller, or that the child is smaller. I do set the size of the child and that is not an issue. The issue is that the child is a subview on another subview and I cannot make it modal. – wayneh Dec 28 '18 at 18:36