当前视图控制器具有UITabBar但没有UITabBarController

时间:2015-07-20 17:38:41

标签: ios swift uiviewcontroller uitabbarcontroller uitabbar

我有UIViewController,其顶部有UITabBar。我没有使用UITabBarController,因为我需要顶部的UITabBar而不是视图的底部。这是我当前的视图控制器:

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时一样)。我怎么能这样做?

1 个答案:

答案 0 :(得分: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
}