ViewController 위에 ViewController 올리기.

|

ViewController 중첩하여 올리기

  1. 뷰를 올리기 621A7D0C-6A8F-4855-8BCA-98F4B7E08656

  2. 뷰 dismiss시 처리하기

0703808D-00DD-4CAB-8F46-2E99C5CDA7AA

  1. 전체 코드
///  ViewController위에 또 다른 ViewController 올리기
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let entryVc = storyboard.instantiateViewController(withIdentifier: storyNewEntry) as! MakeNewEntryViewController
add(entryVc)

entryVc.dismissHandler = {
    self.collectionView.reloadData()
    entryVc.remove()
}

필요한 Extension은 다음과 같습니다.


extension UIViewController {
    func add(_ childVC: UIViewController) {
        addChild(childVC)
        self.view.addSubview(childVC.view)
        childVC.didMove(toParent: self)
    }
    
    func remove() {
        guard self.parent != nil else {
            return
        }
        willMove(toParent: nil)
        removeFromParent()
        self.view.removeFromSuperview()
    }
}

Comments