2016-11-16 133 views
0

我已經嘗試過以下大量示例以及SO上的其他questions。正在添加視圖控制器,但它將視圖容器放入視圖而不是視圖容器。這是我的意思。將視圖控制器添加到容器視圖覆蓋視圖

enter image description here

我有主視圖控制器和容器視圖上的標籤欄。當點擊標籤欄上的項目時,應該抓住控制器並將其加載到容器視圖中。這是我如何做的代碼。

@IBOutlet weak var tabBar: UITabBar! 
@IBOutlet weak var containerView: UIView! 
var currentViewController: UIViewController! 
var radarViewController: UIViewController! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
    let storyboard = self.storyboard 
    let currentViewController = storyboard?.instantiateViewController(withIdentifier: "WeatherCurrentViewController") as! WeatherCurrentViewController 
    self.currentViewController = UINavigationController(rootViewController: currentViewController) 
    let radarViewController = storyboard?.instantiateViewController(withIdentifier: "WeatherRadarViewController") as! WeatherRadarViewController 
    self.radarViewController = UINavigationController(rootViewController: radarViewController) 
} 
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { 
    if item.tag == 1 { 
     containerView.addSubview(currentViewController!.view) 
     currentViewController.didMove(toParentViewController: self) 
    } 

    if item.tag == 2 { 
     containerView.addSubview(radarViewController!.view) 
     radarViewController.didMove(toParentViewController: self) 
    } 
} 

這是Swift 3.我不知道我做錯了什麼。任何幫助表示讚賞。

更新

雖然我從來沒有得到的視圖切換到許多變化/嘗試後編程工作,我沒有找到使用塞格斯多視圖容器的另一種方式(here,不是理想的,因爲它佔用了更多的內存但它的工作原理。

+0

你能解釋一下這進一步「它放置查看容器到視圖中而不是視圖控制器。' – Frankie

+0

@Frankie我看起來不會把它放在視圖容器中,但父視圖 –

回答

0

視圖控制器只是UIView周圍的包裝。所以最後你總會有控制器查看添加爲一個子視圖容器視圖。

的這點是你仍然會得到所有的通知f或視圖控制器,如旋轉變化,佈局...

我使用的類是UIView子類。它看起來是這樣的:

import UIKit 

class ContentControllerView: UIView { 

    weak var parrentViewController: UIViewController? 
    private(set) var currentController: UIViewController? 

    func setViewController(controller: UIViewController) { 
     guard let parrentViewController = parrentViewController else { 
      print("ContentControllerView error: You need to set a parrentViewController to add a new view controller") 
      return 
     } 

     if controller.view != currentController?.view { 
      currentController?.willMove(toParentViewController: nil) // Notify the current controller it will move off the parent 
      controller.willMove(toParentViewController: parrentViewController) // Notify the new controller it will move to the parent 
      parrentViewController.addChildViewController(controller) // Add child controller 
      currentController?.view.removeFromSuperview() 
      currentController?.didMove(toParentViewController: nil) // Notify the current controller it did move off the parent 

      controller.view.translatesAutoresizingMaskIntoConstraints = false // Disable this to add custom constraints 
      self.addSubview(controller.view) // Add as subview 
      // Assign new constraints 
      self.addConstraint(NSLayoutConstraint(item: self, attribute: .left, relatedBy: .equal, toItem: controller.view, attribute: .left, multiplier: 1.0, constant: 0.0)) 
      self.addConstraint(NSLayoutConstraint(item: self, attribute: .right, relatedBy: .equal, toItem: controller.view, attribute: .right, multiplier: 1.0, constant: 0.0)) 
      self.addConstraint(NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: controller.view, attribute: .top, multiplier: 1.0, constant: 0.0)) 
      self.addConstraint(NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: controller.view, attribute: .bottom, multiplier: 1.0, constant: 0.0)) 

      self.layoutIfNeeded() 

      currentController?.removeFromParentViewController() // remove the current controller from parrent 
      controller.didMove(toParentViewController: parrentViewController) // Notify the new controller it did move to the parent 

      currentController = controller 
     } 
    } 

} 

視圖可以在故事板加入,但隨後在代碼中,你需要的視圖控制器分配:

self.contentControllerView.parrentViewController = self 
self.contentControllerView.setViewController(controller: controller) 
+0

感謝馬蒂奇向我展示了你是如何做到這一點的。我真的很想了解我所擁有的代碼是什麼,這不是讓它工作,而只是複製和粘貼別人已經完成的代碼。你可以提供什麼見解? –

+0

嘗試比較呼叫 –

相關問題