这是我的设置.我有一个UIScrollView在我的主视图控制器的顶部,我加载了多个视图控制器.我还有一个Add按钮,它将使用Push segue呈现一个新的视图控制器.
我希望这个视图控制器也只在滚动视图的顶部而不是全屏加载.
到目前为止,我尝试了两种不同的东西,但没有任何工作:
在滚动视图中添加视图控制器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)
}
Run Code Online (Sandbox Code Playgroud)在UIButton操作中添加视图控制器:
@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)
}
Run Code Online (Sandbox Code Playgroud)
这两个解决方案都崩溃了我的应用程序.
有没有办法正确实现这个?
您无法在同一视图控制器上推送任何视图控制器,您需要将容器视图添加到滚动视图.然后,如果你想要,你可以滚动添加按钮的滚动滚动,这样看起来好像新的控制器被添加到它.它可以这样做,
scrollView.contentSize = CGSize(width: screenWidth*3, height: 1)
let first = getStoryboard(StoryboardName.Main).instantiateViewControllerWithIdentifier("FirstViewController") as! FirstViewController
let second = getStoryboard(StoryboardName.Main).instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
let third = getStoryboard(StoryboardName.Main).instantiateViewControllerWithIdentifier("ThirdViewController") as! ThirdViewController
self.addChildViewController(first)
self.scrollView.addSubview(first.view)
first.willMoveToParentViewController(self)
self.addChildViewController(second)
self.scrollView.addSubview(second.view)
second.willMoveToParentViewController(self)
self.addChildViewController(third)
self.scrollView.addSubview(third.view)
third.willMoveToParentViewController(self)
first.view.frame.origin = CGPointZero
second.view.frame.origin = CGPoint(x: screenWidth, y: 0)
third.view.frame.origin = CGPoint(x: 2*screenWidth, y: 0)
Run Code Online (Sandbox Code Playgroud)
如果您只想通过添加按钮添加(移动)到另一个视图控制器,则可能需要禁用滚动视图的滚动.
| 归档时间: |
|
| 查看次数: |
2600 次 |
| 最近记录: |