2016-10-08 3 views
1

これは私のセットアップです。私はUIScrollViewを私のメインビューコントローラの上に複数のビューコントローラをロードしています。私はまた、プッシュセグを使用して新しいView Controllerを表示するAddボタンを持っています。UIScrollView内にUIViewControllerをロードする方法

このビューコントローラーも、すべての画面ではなくスクロールビューの上にのみロードします。
私は今まで、2つの異なるものを試してみましたが、作品のどれも:

  1. 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) 
    } 
    
  2. はUIButtonアクションでビューコントローラを追加します。

@IBAction func addDidTouch(_送信者: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) 
} 

これらのソリューションの両方が私のアプリケーションをクラッシュさせます。
これを正しく実装する方法はありますか?

+0

アップルの[コンテナビューコントローラの実装](https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html)をご覧ください。 –

答えて

4

同じビューコントローラで任意のビューコントローラをプッシュすることはできません。スクロールビューにコンテナビューを追加する必要があります。そして、あなたが望むならば、新しいコントローラが追加されているように見えるように、追加ボタンのタップでスクロールをスクロールすることができます。それはあなただけで、あなたの[追加]ボタンで別のビューコントローラに(移動)を追加したい場合は、あなたのスクロールビューのスクロールを無効にすることも

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) 

、次のように行うことができます。

関連する問題