2015-07-20 78 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

相关问题