             
             viewController.willMove(toParentViewController: nil)
             viewController.view.removeFromSuperview()
-            viewController.willMove(toParentViewController: nil)
+            viewController.removeFromParentViewController()
             
             if automaticallyHandleAppearanceTransitions {
                 viewController.endAppearanceTransition()

        
        // Then add subview, we do this later to prevent viewDidLoad of child view controllers to be called before page segment is allocated.
        for (index, viewController) in viewControllers.enumerated() {
            // Add first view controller to controller hierachy
            // Also, only add first child view controller's view to view hierachy
            if index == 0 {
                viewController.willMove(toParentViewController: self)
                addChildViewController(viewController)
            }
            
            // Add view controller's view if it must be visible in scroll view
            if let _ = indexes.index(of: index) {
                viewController.view.frame = CGRect(x: CGFloat(index) * view.bounds.width, y: 0, width: view.bounds.width, height: view.bounds.height - segmentedControlHeight)
                pageScrollView.addSubview(viewController.view)
            }
            
            if index == 0 {
                viewController.didMove(toParentViewController: self)
            }
        }

        // Call these two methods to notify that two view controllers are being removed or added to container view controller (Check Documentation)
        if automaticallyHandleAppearanceTransitions {
            oldViewController.willMove(toParentViewController: nil)
            addChildViewController(newViewController)
        }
        
        let size = view.bounds.size
        let contentOffset = CGFloat(selectedPageIndex) * size.width
