Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the ChildViewController from Parent View Controller in Swift 3

Tags:

xcode

ios

swift3

I am developing an IOS Application. I have added the UIViewController in View Pager. I want to reinitialize it when the language is changed. Here I want to remove all child UIViewController from UIViewPager and again back to add all UIViewController into Viewpager. How can I do that?

Sample Code

let viewPager = ViewPagerController()
viewPager.options = options
viewPager.dataSource = self
viewPager.delegate = self
self.addChildViewController(viewPager)

Swift 3.1

xcode 8.3.3

like image 685
Noorul Avatar asked Jul 28 '17 07:07

Noorul


People also ask

How do I remove a view controller from storyboard?

To delete the View Controller from the storyboard, select the View Controller by clicking the Show Document Outline icon and then clicking on View Controller Scene in the Document Outline. Then press Backspace or choose Edit > Delete.


3 Answers

After the long search to remove the view controllers from viewpager. I did it in the following way.

 if self.childViewControllers.count > 0{         let viewControllers:[UIViewController] = self.childViewControllers         for viewContoller in viewControllers{             viewContoller.willMove(toParentViewController: nil)             viewContoller.view.removeFromSuperview()             viewContoller.removeFromParentViewController()         }     } 

here self is , Current UIViewController which has View Pager. I need to remove all the childview controllers from the view pager. So, i get the list of view controllers from Current UIViewController. Then i removed it from the Parent view.

For swift 4.2

 if self.children.count > 0{         let viewControllers:[UIViewController] = self.children         for viewContoller in viewControllers{             viewContoller.willMove(toParent: nil)             viewContoller.view.removeFromSuperview()             viewContoller.removeFromParent()         }     } 

EDIT

Remove top childview controller:

   func removeTopChildViewController(){          if self.children.count > 0{              let viewControllers:[UIViewController] = self.children                 viewControllers.last?.willMove(toParent: nil)                 viewControllers.last?.removeFromParent()                 viewControllers.last?.view.removeFromSuperview()          }      } 
like image 125
Noorul Avatar answered Sep 24 '22 06:09

Noorul


childViewControllers.forEach({   $0.willMove(toParentViewController: nil)   $0.view.removeFromSuperview()   $0.removeFromParentViewController() }) 

This answer is for swift 4.2 and above one is less than 4.2 versions.

children.forEach({   $0.willMove(toParent: nil)   $0.view.removeFromSuperview()   $0.removeFromParent() }) 

Here is the more concise version for removing all child controllers from the parent. You can use self.childViewControllers to define explicitly. As this approach, you can get rid of these extra conditions to check.

like image 34
Rehan Ali Avatar answered Sep 26 '22 06:09

Rehan Ali


Swift 4.2 and above

create extension on UIViewController and add removeChild() method

extension UIViewController {
        
  func removeChild() {
    self.children.forEach {
      $0.willMove(toParent: nil)
      $0.view.removeFromSuperview()
      $0.removeFromParent()
    }
  }
} 

Usage:

call removeChild from parent UIViewController

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.removeChild()
}
like image 24
Suhit Patil Avatar answered Sep 24 '22 06:09

Suhit Patil