all white cheat sheet Dana’s Swifty Life

[iOS] Container View Controller - child view controller 추가/삭제하기

 

Container View Controller

다른 vc 들을 담는 vc 로, 자신의 틀 안에서 여러 개의 child vc 를 관리하는 view controller. 이미 존재하는 전체 틀 내에서 특정 부분의 content 만 바뀌어야 되는 구조에서 주로 사용된다. 동시에 한개 혹은 여러개의 vc 를 보여줄 수 있다.

구현된 class

  container 고정 요소 child vc 개수
UINavigationController navigation bar (+ optional tool bar) 1
UITabBarController tab bar 1
UISplitViewController - 2

 

구조

Split View Controller

Tab Bar Controller

 

Child View Controller

  • 접근

    var children: [UIViewController]
    
  • 추가

    1. parent(container vc) 에서 child 추가 - addchild()

    2. Child vc 의 root view 를 parent view 계층구조에 연결 - addSubView() 등…

    3. Child vc 의 root view 의 layout 관련 제약을 추가

    4. child 가 parent 에 추가되었음 - didMove(toParent:)

      // containerVC 내에서
      self.addChild(childVC)
      self.view.addSubView(childVC.view)
      self.view.addConstraints(childVC.view.constraints)
      childVC.didMove(toParent: self)
      
  • 제거

    1. child 에서 parent 제거/이동 - willMove(toParent:) (nil이면 no parent)

    2. Child vc의 constraints 제거

    3. child vc 의 root view 를 parent view 계층에서 제거

    4. child에서 removeFromParent() 호출

      childVC.willMove(toParent: nil)
      childVC.view.removeFromSuperview()
      childVC.removeFromParent()
      

📌 Reference

View Controller Programming Guide for iOS