2016-11-18 10 views
1

既存のビューコントローラをスクロールビューに追加する方法を教えてください。私は 'viewDidLoad'の内部で以下のコードを試しましたが、うまくいきませんでした。ありがとうビューコントローラをスクロールビューに並べて表示する

let view1 = ViewController6() 
     let view2 = ViewController2() 
     let view3 = ViewController3() 

     contentView.addSubview(view1) 
     contentView.addSubview(view2) 
     contentView.addSubview(view3) 

     self.contentView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.height) 
+0

は、コンテナの景色を見てください役立ちます願っています。アイデアは、contentView(UIView)を作成し、現在のViewControllerに子UIViewControllerを追加してから、子ViewControllerのビューをコンテンツビューに追加するというものです。 – uti0mnia

答えて

0

ここで私はあなたが達成したいと思っていることをやっています。

func setupScrollView() 
    { 
     //loop for 
     for i in 0..<3 { 
      //we instantiate our viewController from storyboard 
      let news2 = self.storyboard?.instantiateViewController(withIdentifier: "NewsMasterViewController") as! NewsMasterViewController 
      //we adjust the frame according 
      news2.view.frame = CGRect(x: CGFloat(i) * self.scrollView.frame.size.width + CGFloat(MasterViewController.margin), y: 0, width: self.scrollView.frame.size.width - CGFloat(MasterViewController.margin * 2), height: self.scrollView.frame.size.height) 
      //we call layoutSubviews for constraints adjustments 
      news2.view.layoutSubviews() 
      self.addChildViewController(news2) 
      //added the view of my viewController "news2" to scrollView 
      self.scrollView.addSubview(news2.view) 
      news2.didMove(toParentViewController: self) 
      //added the viewController "news2" to my array of viewControllers 
      newsMasterViewControllers.append(news2) 
     } 

     //adjusted contentSize of scrollView according the number of viewControllers added 
     self.scrollView.contentSize = CGSize(width: self.view.frame.size.width * 3, height: self.scrollView.frame.size.height); 
    } 

私はこれがあなた

関連する問題