This is my setup. I have an UIScrollView
on top of my main view controller in which I load multiple view controllers. I also have an Add button which will present a new view controller using a Push segue.
I want this view controller to also load only on top of the scroll view and not the all screen.
I tried 2 different things until now, but none of the work:
Add view controller inside scroll view in
prepareForSegue
:override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let addViewController = segue.destination as! AddViewController addChildViewController(addViewController) addViewController.view.frame = CGRect(x: 0, y: 0, width: width, height: height) scrollView.addSubview(addViewController.view) didMove(toParentViewController: self) }
Add view controller in UIButton action:
@IBAction func addDidTouch(_ sender: AnyObject) {
let addViewController = AddViewController()
addChildViewController(addViewController)
addViewController.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
scrollView.addSubview(addViewController.view)
didMove(toParentViewController: self)
}
Both of this solutions crashes my app.
Is there a way to implement this correctly ?