2015-07-20 76 views
0

我有一個UITabBar一個UIViewController在它的上面。我沒有使用UITabBarController,因爲我需要的UITabBar在頂部,而不是在視圖的底部。這裏是我當前的視圖控制器:現在視圖控制器,而當前視圖控制器具有中的UITabBar一樣,但沒有的UITabBarController

class CustomTabBarController: UIViewController, UITabBarDelegate 
{ 
    @IBOutlet weak var tabBar: UITabBar! 
    var viewControllers: [UIViewController] = [] 

    //MARK: UIVIewController delegate 
    override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     //TabBarController 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 

     self.viewControllers.append(storyboard.instantiateViewControllerWithIdentifier("myWorldViewController") as UIViewController) 
     self.viewControllers.append(storyboard.instantiateViewControllerWithIdentifier("meViewController") as UIViewController) 
     self.viewControllers.append(storyboard.instantiateViewControllerWithIdentifier("welcomeViewController") as UIViewController) 
     self.tabBar.selectedItem = self.tabBar.items?.first 


     //Graphical Settings 
     let itemWidth = self.tabBar.frame.width/3 as CGFloat 
     let image = self.imageWithColor(UIColor.grayColor(), size: CGSizeMake(itemWidth, 49)) 

     UITabBar.appearance().barTintColor = UIColor.whiteColor() 
     UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 20)!], forState: UIControlState.Normal) 
     UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -10) 
     UITabBar.appearance().tintColor = UIColor.whiteColor() 
     UITabBar.appearance().selectionIndicatorImage = image 

     //Borders 
     self.tabBar.layer.borderColor = UIColor.grayColor().CGColor 
     self.tabBar.layer.borderWidth = 1 

     self.tabBar(self.tabBar, didSelectItem: (self.tabBar.items?.first)!) 
    } 

    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) 
    { 
     self.view.insertSubview(self.viewControllers[item.tag].view, belowSubview: self.tabBar) 
    } 

    override func didReceiveMemoryWarning() 
    { 
     super.didReceiveMemoryWarning() 
    } 

    //MARK: Utils 
    func imageWithColor(color: UIColor, size: CGSize) -> UIImage 
    { 
     let rect = CGRectMake(0, 0, size.width, size.height) 
     UIGraphicsBeginImageContext(size) 
     let context = UIGraphicsGetCurrentContext() 
     CGContextSetFillColorWithColor(context, color.CGColor) 
     CGContextFillRect(context, rect) 
     let image: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return image 
    } 
} 

我現在需要的,它在標籤視圖一個按鈕上點擊時呈現視圖控制器。在目標控制器上,我需要保留UITabBar(例如使用正常的UITabBarController時)。我怎樣才能做到這一點 ?

回答

2

你想create a custom Container View Controller。 您的CustomTabBarController應該是每個選項卡的視圖控制器的父視圖控制器。它將是一個負責顯示UITabBar和下面選定的子視圖控制器的視圖。

嘗試結合下面的代碼到你的類:

let contentView = UIView() 
var selectedViewController: UIViewController? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    view.addSubview(contentView) 

    // ... rest of your code 
    // + layout of contentView to take up the area below tabBar 
} 

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) { 
    if let currentVC = selectedViewController { 
     currentVC.willMoveToParentViewController(nil) 
     currentVC.view.removeFromSuperview() 
     currentVC.removeFromParentViewController() 
    } 
    let newVC = viewControllers[item.tag] 
    addChildViewController(newVC) 
    contentView.addSubview(newVC.view) 
    newVC.view.frame = contentView.bounds 
    newVC.didMoveToParentViewController(self) 
    selectedViewController = newVC 
} 
+0

謝謝您的回答。我將檢查您提供的鏈接以瞭解有關Container View Controller的更多信息,這樣我不僅可以在不知道自己在做什麼的情況下複製代碼。我會盡快將您的答案標記爲「已接受」。 – Aeradriel

+0

在此之後,代碼我仍然有同樣的問題,當我試圖在一個子視圖,標籤欄被「刪除」從一個按鈕推新視圖控制器。的確,這個級別的標籤欄並沒有互動。有沒有辦法通知Container我們想推新的VC? (不使用NSNotification) – Aeradriel

+1

你可以遍歷'parentViewController' /'presentingViewController'屬性,找到了'CustomTabBarController'。或者,如果CustomTabBarController是您的應用程序根視圖控制器,則可以使用'[UIApplication sharedApplication] .keyWindow.rootViewController'來訪問它。使用通知也很好,恕我直言。哪種方式適合您取決於您​​的整體架構。 – jaetzold

相關問題