How to set Custom size to Present Modally ViewController

4k Views Asked by At

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.

3

There are 3 best solutions below

0
On BEST ANSWER

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
4
On

You should add a view of 70 % of size on your second VC (I mean which you are presenting), and set background color of that VC's view as clear color so for that remaining 30 % will display previous VC.

For example your VC's height is 100 px then add another view with height of 70 pixel and set clear background color to self.view I mean main view of VC!

Now set modalPresentationStyle to your VC before presenting it like,

  yourVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;

     [self presentViewController:yourVC animated:YES completion:^{


    }];

If you are giving support lesser than ios 8 then you need to set setModalPresentationStyle to NavigationController(If you are using else parent view controller - which are presenting view controller) like,

     [self.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];

     [self presentViewController:yourVC animated:YES completion:^{


    }];

Swift :

    yourVC.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

    self.presentViewController(yourVC, animated: true) {

    }

or

    self.navigationController?.modalPresentationStyle = UIModalPresentationStyle.CurrentContext

    self.presentViewController(yourVC, animated: true) {

    }
0
On

Have a look into this and let me know if you want any clarification.

__weak IBOutlet UIView *conViewers;

- (void)callingOfCustomView:(UIViewController *)showPushedVC
{

    [showPushedVC.view setTranslatesAutoresizingMaskIntoConstraints:YES];
    showPushedVC.view.frame = conViewers.bounds;
    //**>> Add child view into container view
    [conViewers addSubview: showPushedVC.view];
    [self addChildViewController: showPushedVC];
    [showPushedVC didMoveToParentViewController:self];
}

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];

    ViewersVC *objViewers = [storyboard instantiateViewControllerWithIdentifier:@"ViewersVC"];

    [self callingOfCustomView:objViewers];

Will update this in swift.