Search code examples
iosswiftuistoryboardsegue

How to set Custom size to Present Modally ViewController


I have ViewController and when user clicks at a bottom of it then other ViewController pops-up using segue Present Modally. Is it possible to give custom size to them ? I tried giving Content Size by Use Preferred Explicit Size option but it didn't help. I want that VC to pop up and take 70% of the current VC from up.


Solution

  • Solved it by using UIContainerView instead of "Present Modally" segue.

    This is how I did it in case anyone else is struggling with same issue.

    DO NOT SET ANY SEGUE/EMBED TO VC B/W UIContainerView. Add STORYBOARD ID.

    @IBOutlet weak var containerView: UIView!
    weak var currentViewController: UIViewController?
    
    override func viewDidLoad() {
    
        self.currentViewController = self.storyboard?.instantiateViewController(withIdentifier: "ContainerVC")
        self.currentViewController!.view.translatesAutoresizingMaskIntoConstraints = false
        self.addChildViewController(self.currentViewController!)
        self.addSubview(subView: self.currentViewController!.view, toView: self.containerView)
    
        super.viewDidLoad()
    
    }
    //MARK:- CONTAINER VIEW
    
    func addSubview(subView:UIView, toView parentView:UIView) {
        parentView.addSubview(subView)
    
        var viewBindingsDict = [String: AnyObject]()
        viewBindingsDict["subView"] = subView
        parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[subView]|",
                                                                                 options: [], metrics: nil, views: viewBindingsDict))
        parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[subView]|",
                                                                                 options: [], metrics: nil, views: viewBindingsDict))
    }
    
    func cycleFromViewController(oldViewController: UIViewController, toViewController newViewController: UIViewController) {
        oldViewController.willMove(toParentViewController: nil)
        self.addChildViewController(newViewController)
        self.addSubview(subView: newViewController.view, toView:self.containerView!)
        newViewController.view.alpha = 0
        newViewController.view.layoutIfNeeded()
        UIView.animate(withDuration: 0.5, animations: {
            newViewController.view.alpha = 1
            oldViewController.view.alpha = 0
        },
                                   completion: { finished in
                                    oldViewController.view.removeFromSuperview()
                                    oldViewController.removeFromParentViewController()
                                    newViewController.didMove(toParentViewController: self)
        })
    }
    

    THEN WHENEVER YOU WANT TO CHANGE VIEW ACCORDING TO ACTION

        let newViewController : DestinationVC = self.storyboard?.instantiateViewController(withIdentifier: "DestinationVC") as! DestinationVC
        newViewController.data = dataToBeSentViaSegue // Passing DATA
        newViewController.view.translatesAutoresizingMaskIntoConstraints = false
        self.cycleFromViewController(oldViewController: self.currentViewController!, toViewController: newViewController)
        self.currentViewController = newViewController