2016-12-26 93 views
0

我在使用scrollview的baseview控制器。在那個scrollview中我添加了三個viewcontrollers視圖(xib)。所以我可以在horizantal中滾動這三個。在我的第一個視圖控制器中,我顯示一個tablevieew.While運行我的應用程序,它顯示所有我的3視圖controllers.But當我觸摸我的tableview單元格消失。我已經在xcode 7.3中做了。它工作正常。但在xcode 8中它不工作。請幫我解決這個問題。我重視我的示例代碼在scrollview中添加視圖控制器ios swift

let scrollViewWidth:CGFloat = self.scrollContainer.frame.width 
     let scrollViewHeight:CGFloat = self.scrollContainer.frame.height 
     let x = CGFloat(i) * scrollViewWidth 

     if i == 0{ 
      let qualification = EducationViewController (nibName: "EducationViewController", bundle: nil) 
      //qualification.view.frame.size.height = scrollViewHeight 
      //qualification.view.frame.size.width = scrollViewWidth 
      qualification.view.frame.origin.x = x 
      self.scrollContainer!.addSubview(qualification.view) 
      qualification.didMove(toParentViewController: self) 
     } 
     else if i == 1{ 
      let state = StateRegistrationViewController (nibName: "StateRegistrationViewController", bundle: nil) 
      //state.view.frame.size.height = scrollViewHeight 
      // state.view.frame.size.width = scrollViewWidth 
      state.view.frame.origin.x = x 
      self.scrollContainer!.addSubview(state.view) 
      state.view.backgroundColor = UIColor.red 
      state.didMove(toParentViewController: self) 
     } 
     else if i == 2{ 
      let exp = ExperienceViewController (nibName: "ExperienceViewController", bundle: nil) 
      // exp.view.frame.size.height = scrollViewHeight 
      // exp.view.frame.size.width = scrollViewWidth 
      exp.view.frame.origin.x = x 
      self.scrollContainer!.addSubview(exp.view) 
      exp.view.backgroundColor = UIColor.orange 

      exp.didMove(toParentViewController: self) 
     }} 

回答

0

,同時增加您的自定義視圖 - 控制對滾動視圖,你也應該添加自定義視圖控制器作爲主控制器的子控制器的看法。

像你的情況,你有三個自定義的控制器,然後按在你的代碼,如果其他條款必須添加:

self.addChildViewController(qualification) 
self.addChildViewController(state) 
self.addChildViewController(exp) 
在那裏各塊

在你的代碼。

您的代碼將是這樣的:

let scrollViewWidth:CGFloat = self.scrollContainer.frame.width 
     let scrollViewHeight:CGFloat = self.scrollContainer.frame.height 
     let x = CGFloat(i) * scrollViewWidth 

     if i == 0{ 
      let qualification = EducationViewController (nibName: "EducationViewController", bundle: nil) 
      //qualification.view.frame.size.height = scrollViewHeight 
      //qualification.view.frame.size.width = scrollViewWidth 
      qualification.view.frame.origin.x = x 
      self.scrollContainer!.addSubview(qualification.view) 
      self.addChildViewController(qualification) 
      qualification.didMove(toParentViewController: self) 
     } 
     else if i == 1{ 
      let state = StateRegistrationViewController (nibName: "StateRegistrationViewController", bundle: nil) 
      //state.view.frame.size.height = scrollViewHeight 
      // state.view.frame.size.width = scrollViewWidth 
      state.view.frame.origin.x = x 
      self.scrollContainer!.addSubview(state.view) 
      self.addChildViewController(state) 
      state.view.backgroundColor = UIColor.red 
      state.didMove(toParentViewController: self) 
     } 
     else if i == 2{ 
      let exp = ExperienceViewController (nibName: "ExperienceViewController", bundle: nil) 
      // exp.view.frame.size.height = scrollViewHeight 
      // exp.view.frame.size.width = scrollViewWidth 
      exp.view.frame.origin.x = x 
      self.scrollContainer!.addSubview(exp.view) 
      self.addChildViewController(exp) 
      exp.view.backgroundColor = UIColor.orange 

      exp.didMove(toParentViewController: self) 
     }} 
0
  1. 發起的viewController

    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let aViewController = storyboard.instantiateViewController(withIdentifier: "A") as! AViewController; 
    let bViewController = storyboard.instantiateViewController(withIdentifier: "B") as! BViewController; 
    let cViewController = storyboard.instantiateViewController(withIdentifier: "C") as! CViewController; 
    
  2. 添加viewControllers到一個數組

    let viewControllers = [aViewController, bViewController, cViewController] 
    
  3. 運行的環路增加該viewControllers到了滾動

    var idx:Int = 0 
    for viewController in viewControllers { 
    
        addChildViewController(viewController); 
        let originX:CGFloat = CGFloat(idx) * width; 
        viewController.view.frame = CGRect(x: originX, y: 0, width: width, height: height); 
        scrollView!.addSubview(viewController.view) 
        viewController.didMove(toParentViewController: self) 
        idx += 1; 
    } 
    

全部完成!

相關問題