         // 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)
-            newViewController.willMove(toParentViewController: self)
-        }
-        
-        addChildViewController(newViewController)
+            addChildViewController(newViewController)
+        }
         
         let size = view.bounds.size
         let contentOffset = CGFloat(selectedPageIndex) * size.width

            
            viewController.willMove(toParentViewController: nil)
            viewController.view.removeFromSuperview()
            viewController.willMove(toParentViewController: nil)
            
            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 view controller's view if it must be visible in scroll view
            if let _ = indexes.index(of: index) {
                // Add to call viewDidLoad if needed
                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)
                
                // This will call viewWillAppear
                addChildViewController(viewController)
                
                // This will call viewDidAppear
                viewController.didMove(toParentViewController: self)
            }
        }
