본문 바로가기
iOS/설명

[iOS] Custom Container ViewController

by Sky Titan 2022. 8. 21.
728x90
 

Apple Developer Documentation

 

developer.apple.com

Custom Container ViewController

  • app의 data를 보여주는 Content ViewController들과는 다르게 Container ViewController는 다른 viewController들을 보여주고, screen에 배치하고 그들 사이에 navigation을 핸들링한다.
  • Container ViewController는 여전히 ViewController이기에, window에 띄우거나 다른 ViewController처럼 present할 수도 있다.
  • Container ViewController는 하나 이상의 child ViewController을 통합하여 하나의 View hierarchy에서 관리한다.
    • 각각의 child는 또 본인만의 view hierarchy를 관리하는데, container는 child의 root view의 size와 position을 관리한다.
  • EX) UINavigationController, UITabBarController, UIPageViewController

 

Child ViewController 추가

  1. addChild(_:) 메서드를 container viewController에서 호출해서 containment 관계를 만든다.
    • viewWillAppear와 같은 lifeCycle 메서드가 호출됨
  2. addSubview()로 child의 root view를 container의 view hierarchy에 넣는다.
  3. child의 root view의 position과 size를 정할 수 있도록 제약을 건다.
  4. child의 didMove(toParent:) 메서드를 호출하여서 transition이 완료되었음을 알린다.
override func viewDidLoad() {
        super.viewDidLoad()
        
        let redVc = RedViewController()
        self.redVC = redVc
        addChild(redVc)
        view.insertSubview(redVc.view, belowSubview: closeRedButton)
        redVc.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            view.topAnchor.constraint(equalTo: redVc.view.topAnchor),
            view.leadingAnchor.constraint(equalTo: redVc.view.leadingAnchor),
            view.trailingAnchor.constraint(equalTo: redVc.view.trailingAnchor),
            redVc.view.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 1/2)
        ])
        
        
        redVc.didMove(toParent: self)
        
        let blueVC = BlueViewController()
        self.blueVC = blueVC
        
        addChild(blueVC)
        view.insertSubview(blueVC.view, belowSubview: closeBlueButton)
        blueVC.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            view.bottomAnchor.constraint(equalTo: blueVC.view.bottomAnchor),
            view.leadingAnchor.constraint(equalTo: blueVC.view.leadingAnchor),
            view.trailingAnchor.constraint(equalTo: blueVC.view.trailingAnchor),
            blueVC.view.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 1/2)
        ])
        blueVC.didMove(toParent: self)
    }

 

Child ViewController 제거

  1. child의 willMove(toParent:) 메서드를 파라미터 nil을 넣어 호출한다.
  2. child의 root view의 제약들을 제거하거나 비활성화한다.
  3. child의 root view의 removeFromSuperview()를 호출하여 view hierarchy에서 제거한다.
  4. child의 removeFromParent() 메서드를 호출하여 container-child 관계를 해제한다.
@IBAction func closeRed(_ sender: Any) {
        redVC?.willMove(toParent: nil)
        redVC?.view.removeFromSuperview()
        redVC?.removeFromParent()
    }
    @IBAction func closeBlue(_ sender: Any) {
        blueVC?.willMove(toParent: nil)
        blueVC?.view.removeFromSuperview()
        blueVC?.removeFromParent()
    }

 

728x90

댓글